Can we call a primitive wrapper class as a reference type after boxing ?
I'm also aware of AtomicInteger
, AtomicLong
, AtomicBoolean
and AtomicReference<V>
are mutable.
Integer age = new Integer(23);
Integer old = age;
System.out.println("Age : "+age);
System.out.println("Old : "+ old);
System.out.println("*************");
age = 24;
System.out.println("Age : "+age);
System.out.println("Old : "+ old);
Result
Age : 23
Old : 23
After update ****
Age : 24
Old : 23
I agreed that primitive and its wrappers are immutable. But what is the meaning\purpose of boxing here?
Copied from Wikipedia:
Boxing, otherwise known as wrapping, is the process of placing a primitive type within an object so that the primitive can be used as a reference object.
Your program would have worked in the same way if you used the primitives (the second assignment uses autoboxing, so it does not change anything). Wrappers are, indeed, reference types, but you cannot take advantage of that, because all wrapper classes for the primitives defined in Java are immutable.
Because of that you cannot, for example, send a wrapped
int
into a method, modify it there, and expect the caller to see modifications of the original wrapper. If you need this functionality, you would have to write your own mutable wrappers.