This is a weird behavior:
Integer x; x = 12;
It is the result of java autoboxing .
Some story:
Prior to Java 1.5, it was not allowed. You had primitives ( int, char, byte, boolean, float, double,long ), and the rest in the Java world were classes (including compatible wrappers: Integer, Character, Byte, Booelan, Float, Double, Long ).
The main Java data structures worked with objects, so if you needed to store numbers in a list, you had to "wrap" your value (wrappers are just ordinary classes that contain a primitive type)
For example, it could be my own int shell:
public class Entero {
Nothing special, in general terms, how the wrapper classes are implemented (of course, there are many useful methods)
So, if you want to use it in a list (which contains only objects), you need:
List list =
Call
list.add( 1024 )
Immediately impossible, because 1024 is an int literal, not an object.
Tons of code were written as follows: over the years.
Since Java 1.5 added autoboxing, which is basically syntactic sugar, now the βnew Integer (i) /integer.intValue ()β is introduced under the hood by the compiler, and the code becomes:
list.add( 1024 );
Removing the wrapping process from the source code.
In addition, with the help of generics, you also saved the cast:
List<Integer> list = ....
Mostly generics is a sign to say, βCheck what is being used, like this type and donβt bother me with casting anymore,β but generics are another topic.