There are two ways to convert a Java primitive into an object:
1. Using a constructor.
2. Using a static factory method.
Using a Constructor
Integer numericalValue = new Integer(10); System.out.println(numbericalValue); #> 10
What’s happening here? Surprisingly, the value of “10” that’s passed into a new Integer is a primitive int. This primitive int is autoboxed as a Integer to properly initialize an Integer class.
Using a Static Factory Method
Integer bestNumberEver = Integer.valueOf(8); System.out.println(bestNumberEver); #> 8
What’s happening here? Java is autoboxing the value of 8 by creating an instance of its primitive value into a Integer value. In fact, so much of this is auto-magic with Java now, that you could simply establish the variable as:
Integer bestNumberEver = 8; System.out.println(bestNumberEver); #> 8