aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals in StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Equals in StringBuffer" Watch "Equals in StringBuffer" New topic
Author

Equals in StringBuffer

Ak
Greenhorn

Joined: Apr 10, 2000
Posts: 18

class a {
public static void main( String [] agrs) {
//
// String comparing
//
String a,b;
StringBuffer c,d;
c = new StringBuffer("Hello");
a = new String("Hello");
b = a;
d = c;
System.out.println(b.equals(a));
System.out.println(d.equals(c));
}
}

In the above example the output is

true
true
I have one doubt in this. Equals is not overridden in StringBuffer class so how come the second print stmt is true.
Thanks in advance
Ak
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
You are right. equals() method is not overiding in the StringBuffer class.
By making d=c, you are assigning the referrence of c to d. It means both the objects are pointing to the same object.
That is why you are getting 'true' not that because both the strings are equal.
Try by just changing your code like this.
c = new StringBuffer("Hello");
d = new StringBuffer("Hello");
in this case c.equals(d) you will get 'false'. because the other the contents are same but their reference values are different.
I guess you might have got my point. Pls. correct me is anything wrong.
Surya K
Ram Nath
Greenhorn

Joined: Jun 04, 2000
Posts: 29
Variables d and c are Stringbuffer and you are correct Stringbuffer doesn't override the equals method in Object.
The variable d and c are referring the same reference c.
d.equals(c) does the shallow comparison i.e. checks both point to the same location in the memory.
So it is equal. Whereas for String it checks the contents are equal.
Hope this helps.
Ramnath
Ak
Greenhorn

Joined: Apr 10, 2000
Posts: 18
Thankyou Surya and Ram.
You mean to say that equals first checks the locations (ref number of teh object) and then compares the contents. right..
Correct me if i am wrong.
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
AK,
When we call equals for StringBuffer, since equals() is not overridden in String Buffer the actual method which is used is the equal() in Object class.equals() in Object behaves similar to the == comparison ie whether the objects are same or not.
Since you are assigning c=d both references are same. So this gives true.
Yojana
Greenhorn

Joined: Jun 12, 2000
Posts: 19
Originally posted by Mani:
AK,
When we call equals for StringBuffer, since equals() is not overridden in String Buffer the actual method which is used is the equal() in Object class.equals() in Object behaves similar to the == comparison ie whether the objects are same or not.
Since you are assigning c=d both references are same. So this gives true.
Hi Everybody
According to AK : For StringBuffer both equals() and == compares the reference of the objects Only.
Please clarify?
With regards
Yojana

ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Hi friends
First of all try this link to read about a similer discussion about String and StringBuffer : http://forum.java.sun.com/forum?14@@.ee7791b
This is the equals method in class String : java.lang.Object)" TARGET=_blank rel="nofollow">http://www.javasoft.com/products/jdk/1.2/docs/api/java/lang/String.html#equals(java.lang.Object)
and this is from class Object : java.lang.Object)" TARGET=_blank rel="nofollow">http://www.javasoft.com/products/jdk/1.2/docs/api/java/lang/Object.html#equals(java.lang.Object)
The difference is String overrides the equals method from Object and StringBuffer don't - as simple as this.
Following is a simple sample program that illustrates the behavior's of String and StringBuffer and primitive String.
public class Testreturn {
public static void main(String argv[]){
StringBuffer sb1 = new StringBuffer("Java");
StringBuffer sb2 = new StringBuffer("Java");
String s1 = new String("Ranch");
String s2 = new String("Ranch");
String sp1 = "JavaRanch";
String sp2 = "JavaRanch";
System.out.println("StringBuffer with equals : " + sb1.equals(sb2));
System.out.println("StringBuffer with == : " + (sb1==sb2));
System.out.println("String with equals : " + s1.equals(s2));
System.out.println("String with == : " + (s1==s2));
System.out.println("String primitive with equals : " + sp1.equals(sp2));
System.out.println("String primitive with == : " + (sp1==sp2));
}
}
Hope this helps
Thanks
ARS Kumar.

ARS Kumar, Sun Certified Programmer for Java 2 Platform
http://www.automatedsqa.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals in StringBuffer
 
Similar Threads
String vs StringBuffer
Stingbuffer Class...
Strings...
String Buffer
String and StringBuffer