This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals();" Watch "equals();" New topic
Author

equals();

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
public class test1
{
public static void main(String args[])
{
String s = "hi";
Object obj = s;
System.out.println(s.equals(obj));
System.out.println(obj.equals(s));
}
}


the answer is true,true

why ....anybody explain me pls


Thanks and Regards, Amit Taneja
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Check the API...

In String, equals(Object obj) "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." In this case, obj is indeed a String representing the same sequence of characters. See "instanceof" added to code.

In Object, equals(Object obj) "implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." In this case, s and obj do indeed point to the same object. See "==" added to code.

Refs:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html
[ May 23, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
thanx marc ...

cool answer/reply

but i got surprised that i got answer soo late..in this ..

speedy javaranch....where i used to get answer within 2-4 hours.

anyway someone comeup ..to answer at last...


cheers,
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11
In Object, equals(Object obj) "implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." In this case, s and obj do indeed point to the same object. See "==" added to code.


Just to clarify, since Marc's reply did not stress this: equals(Object) from Object is never called in this example -- only String.equals(Object) implementation is used.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
what u confused me again...

pls clarify....

and one more thing i have doubt in marc code is that

why ("obj is String: " + (obj instanceof String));
is true..

object is the super class of string...

infact it must be true that (string instanceof obj);

??? any comments..
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
instanceof will tell you if a reference is a certain object or any subclass of the object.

Since obj is a reference to a String (even though it is cast as an Object, it is still a String), (obj instanceof String) will return true. (obj instanceof Object) will also return true.

If you use instanceof with an interface name, it will tell you whether a reference points to an object that implements the interface (or any extended interface of it), so (obj instanceof Runnable) will return true if obj implements the Runnable interface.

The purpose of instanceof is to let you know if an object implements a certain functionality. When you look at it in this light, it is easier to predict its results. If you want to call a method on an object, you can just ask if it is an instanceof the class or interface where the method is declared.

Hope this isn't too much at one time!
Maciek Makowski
Greenhorn

Joined: May 17, 2005
Posts: 11
Adding to what Timmy explained, Object version of equals(Object) is never called because of dynamic dispatch of instance methods. I.e. when you call obj.equals(s) the actual version of equals() to execute is determined at run time based on the type of the object referenced by obj and not on the type of the reference itself. The type of reference matters only when you are accessing static members.
[ May 24, 2005: Message edited by: Maciek Makowski ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maciek Makowski:
Just to clarify, since Marc's reply did not stress this: equals(Object) from Object is never called in this example -- only String.equals(Object) implementation is used.

Absolutely right -- I failed to stress that important point.

In this example, a String instance is upcast (through assignment conversion) to type Object. So when calling obj.equals(Object obj), polymorphism will invoke the overridden method in String, because the runtime type of obj is String. (And this is just a different way of explaining what Maciek already said above.)

But I think it's worth noting that if Object's body of equals were used here, this test would still return true because these names actually reference the same object.
[ May 24, 2005: Message edited by: marc weber ]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
pls clarify....

and one more thing i have doubt in marc code is that

why ("obj is String: " + (obj instanceof String));
is true..

object is the super class of string...

infact it must be true that (string instanceof obj);

??? any comments..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by amit taneja:
...infact it must be true that (string instanceof obj); ...

The instanceof operator tests whether the runtime type of an instance is encompassed by a given class.

In your example, "obj" is a name for an instance -- not a class. So assuming that string (with a lowercase "s") is a reference to some object, then "string instanceof obj" will result in the following compilation error...

...cannot find symbol
symbol : class obj

On the other hand, if we use Object along with String (uppercase "S"), then "String instanceof Object" will result in the following compilation error...

...cannot find symbol
symbol : variable String

But "string instanceof Object" will always return true.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by amit taneja:
pls clarify...


Line 1: A new String object "hi" is created and assigned to the variable s which is of type String. The true runtime type of the object is String (this information is stored with the object itself), and the compilation type of the variable s (the reference type) is also String.

Line 2: The object referenced by s is assigned to the variable obj which is of type Object. The true runtime type of the object is still String, but the compilation type of the variable (the reference type) is Object. This is an example of assignment conversion, in which the reference is upcast.

Line 3: The equals method is called on s. Dynamic (runtime) binding invokes the method body associated with the runtime type of s, which is String.

Line 4: The equals method is called on obj. Dynamic (runtime) binding invokes the method body associated with the runtime type of obj, which is String. This is a characteristic of polymorphism.

(I've tried to be careful with the terminolgy here, but please correct me if inaccurate.)
Praveen Durbha
Greenhorn

Joined: May 16, 2005
Posts: 16
Marc & co,

I have another question regarding equals(). We know that every class extends from the main class Object and every class overrides the Object class equals() method. But how do we know/remember the implementation details of the equals method in every class in the API.

For example, consider the Vector class. This class has overridden the equals method. Now, let's say I have a Vector object and a Linked List object and they have the same contents. So if I do a v.equals(l) it will return true. But if I compare the Vector object with a TreeSet object having the same contents they are not equal i.e. v.equals(t) will return false. The API says that equals() method of Vector will return true only when compared with a list object which explains this anamoly.

So, my question is for the exam, do I need to understand the implementation details of equals() method in every class in the API. If not, what are the important classes. To name a few,I know String and Wrapper classes are important.

Thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Praveen Durbha:
...for the exam, do I need to understand the implementation details of equals() method in every class in the API...

It's important to know that certain classes do not override equals (for example StringBuffer). In these cases, the Object definition of equals is invoked.

You should definitely know the role of the equals method in the hashcode contract (which is explained in the API under Object).

And you should understand the behavior of equals vs. == with respect to Strings. (See this article: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1 )

I could be wrong, but I doubt the exam will require you to know specific details of how equals is implemented (other than in Object).
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by amit taneja:
but i got surprised that i got answer soo late..in this ..

speedy javaranch....where i used to get answer within 2-4 hours.

anyway someone comeup ..to answer at last...

You are joking, right? You posted at 1:00 in the morning and got a reply before lunch the same day. I would say that is exceptional.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equals();