- an object representation of a primitive type (like int, double, char, etc.)
- Java is an object-oriented language, but primitive types like int, double, etc. are not objects.
- Sometimes you need an object version of these to work with features like:
- Collections (e.g.,
ArrayList<Integer>
, notArrayList<int>
) - Generics
- null values
- Methods that require objects
- Collections (e.g.,
- used when u need to use primitive types as if they were reference types
Wrapper class instances can be made with constructors or literals
Integer i = new Integer(123); // constructor
Integer i = 123; // literals
System.out.println(123 == 123); // true
System.out.println(new Integer(123) == new Integer(123)); // false
// they both point to different instances -> have to use .equals
System.out.println(new Integer(123).equals(new Integer(123)));