3

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.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47

4 Answers4

1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I agreed with you 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.” – Balaji Reddy Dec 17 '13 at 14:25
  • @BalajiReddy It does not make a difference in this specific case. The most common place where you need boxing is when you wish to use primitives inside collections. Since Java collections take only reference objects, you work around this limitation by "boxing" your primitives into wrappers. Autoboxing lets you do that implicitly (the way that you did in the `age = 24;` assignment). – Sergey Kalinichenko Dec 17 '13 at 14:33
  • Got your point. I also would like to add "primitives" are not part of OOP!. When I do boxing, i'm converting a value type to reference type. If i'm true here then "for example, send a wrapped int into a method, modify it there, and expect the caller to see modifications of the original wrapper" this should be possible right (conceptually speaking).. – Balaji Reddy Dec 17 '13 at 14:39
  • 1
    @BalajiReddy Yes, if you write your own "wrapper", say, `SettableInteger` that has a getter and a setter, you would be able to manually "box" your primitive into a `SettableInteger`, pass it to some method, and get back the modifications. – Sergey Kalinichenko Dec 17 '13 at 14:41
  • ummm. so can i assume this is possible only with user defined wrapper class and not with predefined wrapper classes like Integer.... ? – Balaji Reddy Dec 17 '13 at 14:44
  • @BalajiReddy Correct. `Integer` is immutable, meaning that once you create the object, you cannot change the value that it wraps (i.e. there's no setter). – Sergey Kalinichenko Dec 17 '13 at 14:47
  • Got it. so we can call it as limitations with predefined wrappers? – Balaji Reddy Dec 17 '13 at 14:52
  • 1
    @BalajiReddy It's not a limitation, it is a safety and performance feature (the same is true about Java strings, too). There are more problems with mutable objects inside containers, especially inside hash maps, than one could imagine. Here is a [link to a relevant answer on the subject of immutability](http://stackoverflow.com/a/1253519/335858). – Sergey Kalinichenko Dec 17 '13 at 14:55
  • umm. In case, the object type is mutable then new object will not be created rit? it will exactly act like other reference types? – Balaji Reddy Dec 17 '13 at 15:04
  • @BalajiReddy Mutability has nothing to do with boxing - all objects used for boxing in Java are always immutable. You can use mutability to avoid creating new objects unnecessarily, but nothing happens automatically. – Sergey Kalinichenko Dec 24 '13 at 13:00
1

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.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

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.

Philipp
  • 67,764
  • 9
  • 118
  • 153
0

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.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • I agreed with you 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. – Balaji Reddy Dec 17 '13 at 14:32
  • In most cases it is important to use refernce types in a library for example or to be able to differentiate between having/not having a value (`null`). – Adam Arold Dec 17 '13 at 14:37