Why wrapper class in Java doesn't behave like a reference type?
Asked Answered
F

4

6

I have a huge problem to understand why wrapper class in Java doesn't behave like a reference type. Example:

Integer one = 10;
Integer two = one;
one = 20;
System.out.println(one);
System.out.println(two);

The output will be:

20

10

I thought that two will be 20 like in this example where I create my own class:

class OwnInteger {
        private int integer;

        public OwnInteger(int integer) {
            this.integer = integer;
        }

        public int getInteger() {
            return integer;
        }

        public void setInteger(int integer) {
            this.integer = integer;
        }
    }

    OwnInteger one = new OwnInteger(10);
    OwnInteger two = one;
    one.setInteger(20);
    System.out.println(one.getInteger());
    System.out.println(two.getInteger());

So the question, is Integer wrapper class special? Why does it behave as I have shown in my examples?

Folliculin answered 25/6, 2017 at 12:28 Comment(2)
There's a big difference between one = 20; and one.setInteger(20);, and it has nothing to do with wrapper classes. one = 20; of the first snippet would be equivalent to one = new OwnInteger(20); in the second snippet.Waftage
These classes behave exactly like reference types, probably because they are reference types. When you point one reference to a different object, you don't change the other pointer. The pointers are independent. It's only changes to an object that are visible via any pointer to the same object. Remember that as the JLS says, Java "references ... are pointers".Swipple
S
9

This is exactly the behavior of a reference type. In your example, two references the same object as one after the assignment. However, re-assigning one a new object has no effect on two, which is the behavior that you see.

You will see the same behavior with other reference objects as well, for example

StringBuilder one = new StringBuilder("10");
StringBuilder two = one;
one = new StringBuilder("20");
// two still references StringBuilder with "10"

In order for a reference class to exhibit the behavior when changing one object also changes the other, the class needs to be mutable, like the OwnInteger class in your code, and the code needs to change the object, rather than reassigning it. Wrapper classes, such as Integer, are immutable, so you would not experience that behavior with them.

Stirk answered 25/6, 2017 at 12:32 Comment(0)
S
3

I thought that two will be 20...

nop, when you do this

Integer two = one;
one = 20;

you are actually assigning a new object to the variable one and variable two will no get updated with those changes...

to your class OwnInteger

OwnInteger one = new OwnInteger(10);
OwnInteger two = one;
one.setInteger(20);

they do what you expected because one and two are pointing to the same reference..

code A will be equivalent to

OwnInteger one = new OwnInteger(10);
OwnInteger two = one;
one = new OwnInteger(20); //one.setInteger(20);
Scorn answered 25/6, 2017 at 12:32 Comment(1)
"one and two are pointing to the same reference.." Wrong, they are pointing to the same object.Swipple
C
0
one = 20;

using "boxing", and is actually equivalent to this:

one = Integer.valueOf(20);

In your case valueOf method create a new object and return a reference of that newly created object because previously not exist in cache.

For your own class one and two both reference variable are pointing to same object.

Courtney answered 25/6, 2017 at 12:38 Comment(1)
The same thing would have happened even without autoboxing, which is not the reason for the OP's observation.Swipple
S
0

The other answers so far all are at least partially wrong. The effect you see has nothing to do with autoboxing or mutability. Changing a pointer like your first step and changing an object via a pointer like your second step are entirely different things. Changing the pointer pointed it to a different object. You didn't change both pointers so they pointed to different objects. This happens regardless of mutability or boxing conversion.

Swipple answered 25/6, 2017 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.