*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals with stringbuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals with stringbuffer" Watch "equals with stringbuffer" New topic
Author

equals with stringbuffer

sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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


sona<br />SCJP
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
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

Joined: Mar 22, 2001
Posts: 18
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


--------------------<BR>Java will never die.<P>Kenneth
senthil kumaar
Greenhorn

Joined: Apr 26, 2001
Posts: 12
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

Joined: Feb 14, 2001
Posts: 234
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

Joined: Mar 14, 2001
Posts: 147
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

Joined: Feb 24, 2001
Posts: 34
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

Joined: Feb 14, 2001
Posts: 234
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

Joined: Feb 26, 2001
Posts: 4445
    
    5

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


Junilu - [How to Ask Questions] [How to Answer Questions]
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
est


[This message has been edited by James Du (edited April 29, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals with stringbuffer
 
Similar Threads
string buffer
String Q
String manipulation
String Buffer
string(buffer) comparision...