Results 1 to 7 of 7
  1. #1
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default StringBuffer situation

    Can someone tell me why this is not printing out properly?
    I expect "Late" but get "ate" instead.
    Java Code:
    StringBuffer word=new StringBuffer('L');
     word.append('a');
      word.append('t');
      word.append('e');
      System.out.println(word);
    thank you.

  2. #2
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    I had a very similar problem. You cannot use a char in the line
    StringBuffer word=new StringBuffer('L');
    you must use a String. You will not get a compile error either, it just won't give you what you want. I think java converts it to a number or something.
    regards
    sandor

  3. #3
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    I think you wont get anything in the first program. because it will not run, the constructor details as per JAVA API is
    Java Code:
    StringBuffer()
              Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
    StringBuffer(int length)
              Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.
    StringBuffer(String str)
              Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.

  4. #4
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    goldhouse, as I mentioned. the first program DID run. All I got was 'ate'. I did not get Late. It did compile and it did run.

  5. #5
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default

    The reason that it does run and does compile (thus throwing you off ) is because there is a parameterless constructor, one tha takes aString indicating the initial contents of the StringBuffer and one that takes an int indicating its initial capacity. In this case the compiler selects the int constructor applying a widening primitive conversion to convert the char value 'L' into and int value (JLS.5.1.2. )

  6. #6
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Yes Sandor ,You are right Thanks for the detailed information.

  7. #7
    g25451 is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 12:51 PM
  2. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 09:38 AM
  3. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM
  4. Help with StringBuffer
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •