In this circumstance what is the value of variable y
after the first two statements? I'm assuming it's Integer 7 but my book says automatic unboxing
of objects only occurs with relational operators < >". I'm a little confused how variable Integer y
gets it's value. Does any unboxing
occur in newInteger(x)
?
Integer x = 7;
Integer y = new Integer(x);
println( "x == y" + " is " + (x == y))
Integer x = 7;
In this case, the int
literal 7 is automatically boxed into the Integer
variable x
.
Integer y = new Integer(x);
This involves an automatic unboxing of the Integer
variable x
into an int
(temporary) variable, which is passed to the Integer
constructor. In other words, it is equivalent to:
Integer y = new Integer(x.intValue());
After this statement, y
points to a new object that is different than x
but containing the same int
wrapped value.