• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals Method Doubt......

 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created instance of Two Classes.

1. StringBuffer s1=new StringBuffer("Java Ranch");
2. String s2=new String("Java Ranch");

when i use the below given statement, Should it give a compile time error or should return false or true..?

s2.equals(s1)

Deepak
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will return false!
 
Dhanesh Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I think it will give false. But I didn’t check it. Why I am saying like this means. The equals methods check both object are meaning fully equal or not if both object are refer the same class. I am assure about it . if it is wrong means … correct me.

-----------------
SCJP(preparing)
 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean to say
Both Wrapper class object should be same and the Value. Other wise it will return false...
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc for String:


equals
public boolean equals(Object anObject)Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides:
equals in class Object
Parameters:
anObject - the object to compare this String against.
Returns:
true if the String are equal; false otherwise.
See Also:
compareTo(java.lang.String), equalsIgnoreCase(java.lang.String)



Since StringBuffer is an Object and not a String, the result is false (true if and only if it is a String of the same sequence of characters.
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
i'll clarify you doubt.

1) .equals is used for only to compare secondary datatypes like Objects and Strings.
String s1=new String("javaranch");
String s2=new String("javaranch");
if(s1.equals(s2))
{
s.o.p("Both are same");
}
else
{
s.o.p("Both are not same");

}

2) == is used for only to compare primitive datatypes like byte,int,short,long,float and Double.

Getting me ?

GoodLuck..
Thanks&Regards..
G Sirish Reddy.,
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will return false, but not for a good reason. An implementation detail (actually, many) has erroneously been specified on the API of java.lang.String and must be maintained forever. Technically (and perhaps more appropriately), the behaviour should return true on most optimal API Specification implementations, but that 'room to move' has been taken away.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic