• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

String and StringBuffer

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following code:
public class abc
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2 = new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
}
}

The answer is false false false and I verified it. Why is it not true true and false? Please explain...

Thanks and Regards
Vineet
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your answer program should be like that:

public class abc
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2 = new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1.toString().equals(sb2.toString()));
System.out.println(sb1.toString().equals(sb2.toString()));
System.out.println(sb1.equals(ss1));
}
}
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vineet,
This is a follow-up from Mahesh's message
System.out.println(sb1==sb2) : false because sb1 and sb2 reference to 2 different objects.
System.out.println(sb1.equals(sb2)): false
System.out.println(sb1.equals(ss1)): false
The 2 lines above are false because StringBuffer doesn't override the equals() methods inherited from the Object class. Calling equals() will only check if they reference the same object or not.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am completely agree with Mahesh and son le. Because using
'==' this operator, we cann't compare two different objects.
In every time it would be two values of the same object.
Here sb1 and sb2 are different objects. So when you will
compare two objects definitely you have to convert your
Objects into Strings using 'toString()' method. Here Mahesh
did it.
Thanks,
Sayeed
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic