• 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

String & String Buffer

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Stringconversions{
public static void main(String args[])
{String s1 = "qrst";
String s2 = "wxyz";
StringBuffer sb1 = new StringBuffer("abcd");
int val = 6;
StringBuffer sb2 = new StringBuffer("efgh");
System.out.println(s1 + val);
//System.out.println(sb1 + val); Gives compiler error
//System.out.println(sb1 + sb2); Gives compiler error
System.out.println(s1 + s2);
System.out.println(sb1 + s2);
}}
Can anybody explain why (sb1+s2) doesn't give an error?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ash Rai:
public class Stringconversions{
public static void main(String args[])
{String s1 = "qrst";
String s2 = "wxyz";
StringBuffer sb1 = new StringBuffer("abcd");
int val = 6;
StringBuffer sb2 = new StringBuffer("efgh");
System.out.println(s1 + val);
//System.out.println(sb1 + val); Gives compiler error
//System.out.println(sb1 + sb2); Gives compiler error
System.out.println(s1 + s2);
System.out.println(sb1 + s2);
}}
Can anybody explain why (sb1+s2) doesn't give an error?


As you know, the String object overloads the operators + and +=, but the StringBUffer doesnot.If you have C++ experience , you can remeber the pre- and pos- overload, So sb1+s2 is ok. Moreover, if you code as that everything is ok: sb1.toString()+sb2.toString() or sb1.toString()+sb2 or sb1+sb2.toString()
or sb1.toString()+val.
My points are just reference, and please notify me if you have more correct answer.
regds
George
 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey George,
Thanks. That definately helps!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic