The statement
data.add( new Integer(1) );
explicitly uses a contructor for wrapper class Integer
to construct an object.
Up through Java version 8 this would have been fine.
But in Java version 9, the constructors for the wrapper classes have been deprecated
so now the compiler complains if you try to use them.
The Java compiler expects an object reference. So when it sees
data.add( 44 );
it must supply an object reference for add()
, and automatically
creates an Integer
object containing 44.
This is called autoboxing.
Unboxing works the other direction.
The int
inside a wrapper object is automatically extracted
if an expression requires an int
.
The following
int sum = 24 + data.get(1) ;
extracts the int
in cell 1 of data and adds it to the primitive int
24.
Does the following to work?
Double value = 2.5; double sum = value + 5.7;