Can someone tell me why this is not printing out properly?
I expect "Late" but get "ate" instead.
thank you.Code:StringBuffer word=new StringBuffer('L');
word.append('a');
word.append('t');
word.append('e');
System.out.println(word);
Printable View
Can someone tell me why this is not printing out properly?
I expect "Late" but get "ate" instead.
thank you.Code:StringBuffer word=new StringBuffer('L');
word.append('a');
word.append('t');
word.append('e');
System.out.println(word);
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
I think you wont get anything in the first program. because it will not run, the constructor details as per JAVA API is
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.
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.
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. )
Yes Sandor ,You are right Thanks for the detailed information.
how are you