• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stringbuffer

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class lal
{
public void fit(StringBuffer l)
{
String g=l+"nitin";
System.out.println(g);
}
public static void main(String[]args)
{
lal l=new lal();
StringBuffer b=new StringBuffer("funkid");
l.fit(b);
System.out.println(b);
}
}
The output in is
1)funkidnitin
2)funkid.
i want's to confirm onething, how does the (funkidnitin)is coming.My thinking say's when i use the + operator in line number 5, string literal convert's the whole experesion in string because string literal "nitin" create's an object which call's the default version of the toString() method of the string clas.Please correct me if i am wrong.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi nitin
as far as i know that when u have : Object+"", the toString() method of the Object is called, so
StringBuffer b=new StringBuffer("funkid");
System.out.println(b.toString());
prints: funkid
hope that helps
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 4 + operator sees that one of it's operands is a String, so it calls toString() on it's left operand (which of course returns a string) and then concatenates the two string ie. funkid and nitin. So g is assigned the value of funkidnitin, which gets printed.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
reply
    Bookmark Topic Watch Topic
  • New Topic