• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding equal() method  RSS feed

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far I know, an overridden equal method check the equivalence of value and type not the object reference.

Please consider the following question:

Integer i = new Integer (42);
Long l = new Long (42);
Double d = new Double (42.0);
Which two expressions evaluate to True? (Choose Two)
A. (i ==l)
B. (i == d)
C. (d == l)
D. (i.equals (d))
E. (d.equals (i))
F. (i.equals (42))
Answer: D, E

I understand the first three are wrong as the memory location of these objects are not same.

But in c and d, the value is same but type is different? Long and Double?
and What's wrong with F?

Or I have problem with understanding wrapper class?
Is the value of a wrapper class converted into a string?

Any helpful reply would be great for me!
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, does the following compile?

Long 1 = new Long (42);


Ok, if you change 1 to l, I think f should be right cause they are both equivalent Integers.

Chan.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you copied the question correctly ? As Chan said the Long declaration won't even compile.
If the question is correct then the answers given are wrong.

From the Integer javadoc
The result is true if and only if the argument is not null and is an Integer object ...

From the Long javadoc
The result is true if and only if the argument is not null and is a Long object...


So D and E are will never evaluate to true.
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:First, does the following compile?

Long 1 = new Long (42);


Ok, if you change 1 to l, I think f is wrong cause


Yes, it was a typing mistake because I copied the code form a source. I've fixed it.

Thanks for your explanation of option f. I have still problem in understanding d and e.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:I think f is wrong cause



creates a new Integer object which is not equal to the cached Integer you get with autoboxing 42 to an Integer - it's a different pre-created object.

Chan.

No. The equals method compares the value of the Integer, not whether they are the same object or not. F will evaluate to true.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


On my machine, prints

false
false
true
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart,

that was a typo too ( answer for == though what was asked was equals ). I was momentarily confused. Ignore that, please.

Chan.


Edit : Even == prints true. Heck I have to revise autoboxing and unboxing again. So @OP - both i.equals(42) and i==42 would print true. Sorry for confusing you.
So now I am left to figure why i==42 gives a true.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jahangir Alam wrote:Please consider the following question:
Answer: D, E

Well, first off, that answer is WRONG, because i.equals(42) will definitely return true. It's possible that E might as well (to be honest, I've forgotten all those involved rules about boxing and unboxing; and I certainly don't rely on them).

My advice: re-check your source and re-submit, because either they're wrong, or you copied it wrong.

Winston
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jahangir Alam wrote:Please consider the following question:
Answer: D, E



My advice: re-check your source and re-submit, because either they're wrong, or you copied it wrong.

Winston


I was actually confused. Today I took part in a test and the teacher provided this answer. But I was truly disagree with it. After getting back home, I have been thinking about this, and to become sure I shared with you.

Please check my first post. I explained that I knew the equal method checks the value as well as type. So the answer must be f.

Actually there are so many mock questions over the net which are provided with such wrong answer. But it is very much confusing for people like me who are in very begging level.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the so many incorrect responses.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!