• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

equals

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
Integer nA = new Integer(4096);
Long nB = new Long(4096);
if (nA.equals(nB)) System.out.println("equals");
if (nA.intValue() == nB.longValue()){
system.out.println("EQ");
}
</pre>
What will be the result ?
A) compiler will object to line 4 because the object types of nA and Nb don't match
B) Program will compile and run , producing "EQ"
C) Program will compile and run , producing "equalsEQ" .............................answer
D) Program will compile and run , producing "equals"

trying hard to get into memory addresses...!!! please help me
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer nA = new Integer(4096);
Long nB = new Long(4096);
if (nA.equals(nB)) System.out.println("equals");
if (nA.intValue() == nB.longValue()){
system.out.println("EQ");
}
Both are object
+----------+
nA---->| 4096 | Say nA's memory address is XYZ
| (32 bit) |
+----------+
+----------+
nB---->| 4096 | Say nB's memory address is ABC
| (64 bit) |
+----------+

Now point to remember: Objects are compared by equals method.
let's see how:
1. if i do: nA== nB, then i will be doing XYZ == ABC
2. if i do: nA.equals(nB) then i will be doing a object comparision which is fine.
3, if i do: (nA.intValue() == nB.longValue()), then
i will be calling nA.intValue() to return int value, and nB.longValue() to return long value, eventually
int (value now is primitive, not object) == long (value now is primitive, not object)
so it will be 4096 == 4096, Yup it is equal !
Any correction is welcome !
 
Dpk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object Diagram:
nA
|
|
+------+
| 4096 | Say nA's memory address is XYZ
| 32bit|
+------+
nB
|
|
+------+
| 4096 | Say nB's memory address is ABC
| 64bit|
+------+
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the correct answer is (B). If you see the contract of boolean equals(Object object) method in Integer class, it accepts only Integer objects to return a true (and ofcourse the value should be same).
So, the third line in the segment returns false.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought B was the answer too, for the same reason - they're not the same object, nor are they both Integers with the same int value. Just to check, I compiled and ran it and it only prints out "EQ".
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic