Eg: int vs Integer
int is primitive type, while Integer is a class (eg: Integer.parseInt("1") is a call of Integer's class method)
All primitive type in Java has an equivalent wrapper class:
- byte -> Byte
- short -> Short
- int -> Integer
- long -> Long
- boolean -> Boolean
- char -> Character
- float -> Float
- double -> Double
Wrapper class inherit from Object class and primitive don't.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
Auto boxing example
List<Integer> li = new ArrayList<>();
int i = 2;
li.add(i);
This is actually
li.add(Integer.valueOf(i));
Unboxing example
int m = li.get(0);
This is actually
int m = li.get(0).intValue();