You can get the address of an object
by converting an object reference to an int,
and you can convert the address of an object,
given as an int,
to an object reference.
Object o = (Object)12345678;
int i = (int)new Object();.
One can cast between references and ints
One cannot cast between references and ints
CorrectionHere is what's right.
Java is a type-safe language. An object reference is opaque; there is no way to convert it to an integer address.
However, Java allows auto-boxing and auto-unboxing.
Which means that Java automatically can
e.g., convert an
int into an
Integer and vice versa.
This doesn’t have anything to do with the object’s address, however!
Integer i = 5; is correct.
And given that
Object is a supertype of
Object o = 5; is correct,
Object o = (Object)5; likewise.
int i = new Integer(5); is correct,
int i = (int)new Integer(5);' is correct.
int i = (int)new Object(); is not correct,
Object is not a subtype of
new Object() can’t possibly be cast into an
for unboxing into an