Comparing 2 objects using equals method

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Comparing 2 objects using equals method

Post 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?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Comparing 2 objects using equals method

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: Comparing 2 objects using equals method

Post 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?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Comparing 2 objects using equals method

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: Comparing 2 objects using equals method

Post by gparLondon »

I am sorry I dint get your second point. Where should I look for explanation on == by you?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Comparing 2 objects using equals method

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 241 guests