Why doesn't the shell class in Java behave like a reference type?

I have a huge problem to understand why the wrapper class in Java does not 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

ten

I thought two would be 20, as in this example, when 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, a special Integer shell class? Why does he behave as I showed in my examples?

+5
source share
4 answers

This is just the behavior of the reference type. In your example, two refers to the same object as one after assignment. However, when re-assigning one new object does not affect two ; this is the behavior you see.

You will see the same behavior with other link objects, for example

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

In order for the reference class to show behavior when one object changes, it also changes another, the class must be changed, like the OwnInteger class in your code, and the code must change the object, rather than reassigning it. Wrapper classes such as Integer are immutable, so you won't run into this behavior.

+8
source

I thought two would be 20 ...

nop when you do it

 Integer two = one; one = 20; 

you are actually assigning a new object to the one variable, and the two variable will not be updated with these changes ...

your OwnInteger class

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

they do what you expected because one and two point to the same link ..

code A will be equivalent

 OwnInteger one = new OwnInteger(10); OwnInteger two = one; one = new OwnInteger(20); //one.setInteger(20); 
+3
source
 one = 20; 

using boxing, and is actually equivalent to this:

 one = Integer.valueOf(20); 

In your case, the valueOf method creates a new object and returns a link to this newly created object, because it did not exist in the cache before.

For your own class, one and two both reference variables point to the same object.

0
source

Other answers are still at least partially erroneous. The effect you see has nothing to do with autoboxing or volatility. Changing a pointer, such as your first step, and changing an object with a pointer, such as your second step, are completely different things. A pointer change pointed to another object. You did not change both pointers, so you pointed to different objects. This happens regardless of the variation or transformation of the box.

0
source

Source: https://habr.com/ru/post/1269204/


All Articles