How does auto boxing/unboxing work in Java?

When in doubt, check the bytecode: Integer n = 42; becomes: 0: bipush 42 2: invokestatic #16 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 So in actuality, valueOf() is used as opposed to the constructor (and the same goes for the other wrapper classes). This is beneficial since it allows for caching, and doesn’t force the creation … Read more