Clarification on Boxing

144 views Asked by At

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.

4

There are 4 answers

10
Sergey Kalinichenko On

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.

0
Philipp On

All boxed wrapper classes are immutable. When you change the value of ageto 24, you don't change the Integer object age points to. You create a new Integer(24) and assign this one to age. The variable old still points to the Integer(23).

When you need a boxed type which is mutable, you can use the Atomic*-variants of them, like AtomicInteger.

2
Adam Arold On

Here you are creating a new instance of Integer explicitly:

Integer age = new Integer(23);

Here you reassign the reference to age. Java implicitly boxes 24 to an Integer instance:

age = 24;

So there is no boxing involved in the first case and since you cannot change a value (because it is immutable) there are actually two Integer objects created one for 23 and one for 24.

0
DwB On

Boxing (or autoboxing) was added because careless goofballs were unwilling (perhaps unable) to recognize the need for code where the type of the lvalue matched the type of the rvalue in cases involving primitive types and primitive wrapper classes.

In your code, here is a hypothetical transcript of the compilers thinking at the line age = 24.

  1. the lvalue is age which is a reference to an integer.
  2. equal sign is assignment.
  3. 24 is a literal integer value.
  4. hangon, this goof is attempting to assign a primitive value to an object reference, that will not stand.
  5. lets create a new Integer object and initialize it with the value 24 then assign that to the lvalue.

The line

a = 24;

is functionally the same as

a = new Integer(24);

That is the meaning and purpose of boxing.