Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2007, 11:41 PM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
StringBuffer situation
Can someone tell me why this is not printing out properly?
I expect "Late" but get "ate" instead.
Code:
StringBuffer word=new StringBuffer('L'); word.append('a'); word.append('t'); word.append('e'); System.out.println(word);
thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-26-2007, 11:56 PM
Member
 
Join Date: Apr 2007
Location: Pennsylvania,USA
Posts: 46
sandor is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-27-2007, 04:10 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-27-2007, 05:27 PM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-27-2007, 06:32 PM
Member
 
Join Date: Apr 2007
Location: Pennsylvania,USA
Posts: 46
sandor is on a distinguished road
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. )
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-27-2007, 09:47 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Yes Sandor ,You are right Thanks for the detailed information.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-12-2008, 03:39 PM
Member
 
Join Date: Aug 2008
Posts: 11
g25451 is on a distinguished road
how are you
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between StringBuilder & StringBuffer Pooja Deshpande New To Java 5 04-16-2008 02:51 PM
StringBuilder v/s StringBuffer Pooja Deshpande New To Java 9 04-11-2008 11:38 AM
StringBuffer Java Tip Java Tips 0 11-08-2007 10:33 AM
Help with StringBuffer Marcus AWT / Swing 2 07-04-2007 07:50 AM


All times are GMT +3. The time now is 01:57 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org