Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String class and StringBuffer class

 
JOSEPH BIH
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone answer the following question? Thanks!
There is no problem comparing string objects, but why comparing stringbuffer objects are not allowed or it returns false.
StringBuffer sb1 = new StringBuffer("Aha");
StringBuffer sb2 = new StringBuffer("Aha");
String str = "Aha";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(str));
System.out.println("Pear".substring(3));
Answer: false false false r
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the problem - StringBuffer doesn't have an equals() method , so it get's the equals() from the Object class .
From the API
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Hope that helps
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add more to Asish's Reply
StringBuffer class does not override equals method which it had inherited from object class.
The object class equals() does shallow comparison; that is checking the references whether thay are same.. which by all means same as == method.(Of course there are differences in the efficiency, speed of these two methods doing the same thing.. but as for as SCJP is concerned they do the same shallow comparison)
For the equals method to do deep comparison such as checking the contents rathser than object references they need to be overridden.
String classes & Wrapper classes override the equals() todo deep comparison.
HTH
Any comments.....Welcome.
tvs sundaram
 
JOSEPH BIH
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys!
/bStringBuffer class does not override equals method which it had inherited from object class.b/
So, Sundaram. Only the Wrapper and String class have their own eauals() methods that can overwrite the Object class equals() methods?
Joseph
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my knowledge; Yes ; Only these two. Ranch gurus can add comments pls.
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll have to agree to that . BTW what is a shallow comparision & a deep comparison , my book sez nothin 'bout these .
Thanx
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Ashish,
Shallow comparison:
Finding out whether two object references share the same memory address.
Deep comparison:
Finding out the whether the contents of the two object references are same.
(Of course there are other conditions like that there will be check whether the operands are really objects, whether they belong to the same type)
HTH
I think RHE uses deep, shallow phrases
tvs
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Sundaram
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic