• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Equals in StringBuffer

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 : http://www.javasoft.com/products/jdk/1.2/docs/api/java/lang/String.html#equals(java.lang.Object)
and this is from class Object : 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.
reply
    Bookmark Topic Watch Topic
  • New Topic