What are boxed types in Java

Autoboxing and unboxing are methods for the automatic 'packing' and 'unpacking' of primitive data types into reference types. v.5.0

Since version 5.0, Java has provided a mechanism that takes care of these processes automatically (Car boxing), so that the following formulations are possible:

Integer i = new integer (5);
int n = i;
Integer j = n;

Here is what happens: a Wrapper class object Integer is formed by passing the value 5 to the constructor. The primitive type n is then initialized with this object by first automatically unboxing the reference type. So there is a change in the reference type i into the primitive type, without explicit casting or unboxing having to be formulated. In the third step, the opposite way is shown: the primitive one int-Type will go through without casting Boxing in the reference type of the wrapper class Integer encapsulated again.
You have to be aware that in any case Boxing and Unboxingprocesses run in the background. It is therefore not recommended in particular for computationally intensive and / or time-critical applications.
Another treacherous peculiarity is shown e.g. in the following code examples and is intended to make it clear that - as Java recommends - all (un) boxing processes should only be carried out when absolutely necessary:

Integer m = 1500; Integer n = 1500; System.out.println (m == n); // false

Here, boxing becomes two Integer-Objects formed, therefore their comparison false results because they are two different objects with correspondingly different storage locations.

Integer i = 15; Integer j = 15; System.out.println (i == j); // true

Amazingly, the comparison of two wrapper objects created as in the previous example generates true as a result. How can this be explained?
The solution is that for values ​​within the byte value range (-128 to 127), if they match, newly created wrapper objects are filled with existing ones, so the comparison here points to one and the same memory location. One speaks of one pool. This principle applies to all integer numeric wrapper data types inclusive Characterbut not for the floating point wrappers.

Integer k = new integer (15); Integer l = new integer (15); System.out.println (k == l); // false

This example takes up the penultimate and demonstrates that the use of the addressed object pool only applies if the wrapper objects are not included new were formed. In this case, through new the newly formed objects inevitably each assigned new storage space.