Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals with stringbuffer

 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
System.out.println("Poddar".substring(3));
outpur
false
false
false
dar
why is the output false for (sb1.equals(sb2))
(sb1.equals(ss1))
please explain
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just like the other post. Its the same concept.
(sb1.equals(sb2))
will print false because you are comparing 2 objects that point to different memory addresses. What i think you want to do is compare 2 strings to see if they are the same.

(sb1.equals(ss1))
Now for this you are comparing an object with a string. They both need to be strings to print out true. Comparing different types will always print false.

[This message has been edited by Andy Ceponis (edited April 26, 2001).]
 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my explaination. The equals() method of StringBuffer in sona's example is inherited from the class Object which is to compare the references of 2 objects.
class A{
public static void main(){
A a = new a();
StringBuffer strB = new B();

a.equals(b);// Using the equals() of Object class
strB.equals(a);// same as above
}
}
Only the equals() method of class String can be used to compare the contents of 2 string objects.
Correct me if i'm wrong.

Kenneth Ho
 
senthil kumaar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the StringBuffer class doesn't override the method equals(), so if u invoke a method equals() in StringBuffer which inturn calls the equals() present in Object class, in Object class the equals() check only that both the refernces are pointing to the same Object.
Because of this if u got the result as false.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is still not clear tome
could someone explain it more clearly
and if i have to override equals here how do i
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually senthil's answer is perfect ...
Since the StringBuffer class does not provide an implementation
of the equals() method so the equals() method of the Object class is used.
In the Object class the implementation of the equals methodchecks to see if the two references point to the same object which is the same as obj1==obj2.
In the String class the equals() method is implemented in such a way so as to return true if the two Strings contain the same exact sequence of characters
Hope this helps
Samith Nambiar
----------------------------------------------------------
The harder i try the luckier i get
----------------------------------------------------------
 
Susan Delph
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a Java Newbie, so this may - or may not - be correct, but....
You asked:
why is the output false for (sb1.equals(sb2)) and (sb1.equals(ss1))
The equals() method would only return true if sb1 and sb2 refer to the same object. But they don't. While the letters ("Amit") may be the same, sb1 and sb2 actually occupy different memory locations.
The same is true with sb1 and ss1. They're in different memory locations.
Senthil's reply, as Samith said, explains this very well: The String class overrides the equals() method, so that if 2 Strings have the same contents, the result is 'true'. But the StringBuffer class doesn't offer this override, so the result is 'false'.
Susan

[This message has been edited by Susan Delph (edited April 28, 2001).]
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everobody i did understand all that u explained
but could someone please show me how to overide the equals method for stringbuffer class
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer is a final class. That means you can't override the behavior of any of its methods in a subclass because you can't subclass it. The solution is to use the StringBuffer.toString() method to compare the values:
<pre>
StringBuffer sb1 = new StringBuffer("foo");
StringBuffer sb2 = new StringBuffer("foo");
if ( sb1.toString().equals(sb2.toString()) ) // true
...
</pre>
Junilu
Originally posted by sona nagee:
thanks everobody i did understand all that u explained
but could someone please show me how to overide the equals method for stringbuffer class

 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
est


[This message has been edited by James Du (edited April 29, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic