• 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 and .equals

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the following:
public class test {
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
boolean result = sb1.equals(sb2);
System.out.println(result);
}
}
Why does it print false???
If you change StringBuffer to String and run it, it will print
true. Please explain this burning question. Thanks. Am I
missing some simple concept?
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default equals() method as defined in the Object class compares object to itself, i.e., as if the equality operator == had been used. This means that if a class does not override equals() method in the Object class, then object value equality is same as object reference equality.
But there is an exception. Certain classes like String, Date, File, all wrapper classes (like Interger, Boolean, Double) equal() method is defined to check value equality. For StringBuffer class it looks like equal() method is not defined. Hence the default method from Object class tests for referential equality. That is the reason you get false when two StringBuffer objects are tested for equality.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satish Sangapu:
Check out the following:
public class test {
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
boolean result = sb1.equals(sb2);
System.out.println(result);
}
}
Why does it print false???
If you change StringBuffer to String and run it, it will print
true. Please explain this burning question. Thanks. Am I
missing some simple concept?



StringBuffer class dosen't override equals() method
so it is having the same equals() method that is inherited by Object class. so it will just check for the object reference
In above case sb1 and sb2 are different objects. so it returns false.
Whereas String class Overrides equals()method to check for the contents of the class..
so it returns true..
 
reply
    Bookmark Topic Watch Topic
  • New Topic