• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Equals Method Doubt......

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will return false!
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.,
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic