Page 1 of 1
Comparing 2 objects using equals method
Posted: Thu Nov 27, 2014 3:59 am
by gparLondon
Code: Select all
package com.oracle.certification.enthuware;
public class ObjPrimEqualsTest {
public static void main(String args[])
{
short i=3;
Short j=3;
Short s=new Short(i);
Short ss=new Short(j);
Integer iprim=new Integer(s);
Integer iobj=new Integer(ss);
System.out.println(iprim.equals(s));//o/p false
System.out.println(iobj.equals(ss));//o/p false
System.out.println(ss.equals(j));//o/p true
}
}
Hi Paul, Can you explain me this behaviour of my program with your cup and remote control theory?
Re: Comparing 2 objects using equals method
Posted: Thu Nov 27, 2014 4:09 am
by admin
Not sure which theory are you referring to but can you tell me which part do you have a problem with? The code seems quite straightforward to me. Just remember that what equals method does depends on how it is coded for that particular class. So take a look at the JavaDoc for Integer class's equals method and see what it says. I am sure it will be clear to you.
Re: Comparing 2 objects using equals method
Posted: Fri Nov 28, 2014 12:41 am
by gparLondon
Hi,
This link below
http://www.javaranch.com/campfire/StoryPassBy.jsp
was answered to the question,
viewtopic.php?f=2&t=1185
Explains that, Objects are passed with reference to the method. Now my understanding of this theory, correct me if I am wrong.
- In my program I have 6 cups, named i,j,s,ss,iprim,iobj.
- Value of i is a primitive which is 3.
- Value of j is a object which is 3. might be something like an address 3 in the cup j, which is not pointing to anything.
- Value of s is copy of primitive created on the heap now object value 3, and cup s contains say for example address add1.
- I am not sure what happens to ss. Maybe because ss is an object it is created on the heap as an object 3 Say for ex: its address is add2 now, value in the cup ss is add2.
- Value of s is passed to Integer object iprim now cup iprim value is add1.
- Value of ss is passed to Integer object iobj now cup iobj value is add2.
As equals method compares for a reference, i.e address why are they not equal? i.e my first and second o/p, why it isn't true?
Re: Comparing 2 objects using equals method
Posted: Fri Nov 28, 2014 1:41 am
by admin
Couple of points:
1. The article is about Objects and not primitives. Object references are passed by value (so it feels like objects are passed by reference). Primitive values are passed directly i.e. their references are not passed. Please re read the article. (It is not written by us but is good nevertheless.)
As equals method compares for a reference
2. Where did you read this?? Please read my response regarding equals method above. == operator compares reference.
Once you get the about two points clear in your head, the code is realy simple to work out.
-Paul.
Re: Comparing 2 objects using equals method
Posted: Fri Nov 28, 2014 5:50 am
by gparLondon
I am sorry I dint get your second point. Where should I look for explanation on == by you?
Re: Comparing 2 objects using equals method
Posted: Fri Nov 28, 2014 8:01 am
by admin
I said, "Please read my response regarding equals method above. For == operator there is no other explanation by me other than that it compares references.
HTH,
Paul.