Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

StringBuffer == and equals

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyClass {
public static void main(String args[]) {

StringBuffer sb = new StringBuffer("hello");
StringBuffer sb1 = new StringBuffer("hello");
if(sb == sb1) System.out.println("a");
if(sb .equals(sb1)) System.out.println("b");

}
}
I was solving Amit Poddar's question's on String. In question 1 he has given similar code which I gave above. But his answers includes
if(sb == sb1) System.out.println("a"); //false
if(sb .equals(sb1)) System.out.println("b"); //false
But when I compiled above code I got no output. I checked APT doc also and I found that StringBuffer has no equals method. So If i get similar quetion in exam which answer shoud I pick?
Thanks,
Ash
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ash,
The equals method is specified in the Object class and checks if two object references actually point to the the same object. It does not check the contents of the objects. Other classes override the equals method to provide that functionality. For example the String class overrides the equals method to check if two strings are the same - contentwise.
The Stringbuffer class does not override the equals method so Stringbuffer objects use the equals method in the Object class and hence the result of sb1 not being equal to sb2 as they are different objects and nothing prints
The == method checks if two object reference point to the same object. For primitives it checks the contents.
Hope this helps.
Thanks
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ash,
First of all, EVERY object in Java has an equals() method, as it is defined in the Object class, and every class in java implicitly extends Object. So, StringBuffer does have a equals() method, which it inherits from Object.
Regarding the code snippet, no wonder you get no output. You are not supposed to get output according to the code given. Output is obtained only if the condition in the if statements are true.
As both if statements are evaluated to false, you don't get any output. the sb==sb1 statement evaluates to false because the two references refer to different objects, and the sb.equals(sb1) statement evaluates to false because the equals() method is not overridden in the StringBuffer, and hence the equals() from Object is called, which performs a shallow comparison(equivalent to sb==sb1).
I have made some changes in the System.out.println() statements in the code, and the output is obtained.

I hope it clears your doubt.
Prasanna.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand all of the part about equals and == and what they actually compare, however...
If both SringBuffers are created using "hello" isn't that an absolute string which there should only be one of in the Sring table? Or, does that not apply because it is a StringBuffer?
Thanks
Dave
 
Ash sav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks nagarajan , Prasanna, and Dave.
 
reply
    Bookmark Topic Watch Topic
  • New Topic