简体   繁体   中英

What happens when 2 objects are equal to each other

I need a little help in java. I'm just a beginner of java. But I believed my foundation was pretty solid.

I was creating a simple java application when I encountered a slight problem with 2 of my ArrayList objects. Lets call them list1 and list2 . If I were to make list1 equal to list2 list1 = list2; and add an element to list2 list2.add(obj) , the element is added to list1 as well.

I did my research and found out I should do this instead list1 = new ArrayList(list2); I didnt know java objects work like pointers. I thought only the values are being passed when 2 objects are equaled. I even created a simple test application which can set and get a number of an object. Again, I equaled both objects. Changing the element of 1 object seems to affect the other object as well. I dont know how I should search this on Google. Which is why I'm feeding you the whole story. I only get documents related to c programming. I feel like my foundation just got broken to pieces. I just down know how the = works now. Tried doing

    int num1 = 666;
    int num2 = num1;
    num1 = 42;

This doesnt affect num2. However,

    Object obj1 = new Object();
    Object obj2 = obj1;
    obj1.changeSomeElement();

This affects obj2.
Now, I'm confused on how the = works in java. Someone please share some useful docs for me to read please. Thanks!

int num1 = 666;
int num2 = num1;
num1 = 42;

This works because int is a primitive data type

Object obj1 = new Object();
Object obj2 = obj1;
obj1.changeSomeElement();

This does not work because Object is a reference data type

obj1 is holding a reference to the created Object, and you are just passing that same reference from obj1 to obj2

That is, both obj1 & obj2 refer to the same Object

Changes are made to the same Object whether you use obj1 or obj2 :)

Primtive vs Reference Data Types

A very good explanation on primitive vs reference data types as well as their implications

In Java variables are references to objects. So when you say:

Object obj2 = obj1;

obj2 stores the same reference as obj1. This is why if you modify the object referenced by obj1 you are also editing the object referenced by obj2 (it's the same object referenced by two variables).

In your first case, you changed num1 by assigning it again. In your second, you called a method on your object.

Now a more precise answer: when you assign an object (= is your assignment operator), what you do is essentially say 'this points at this object in memory'.

Hence, when you do obj1 = obj2 , you use two pointers to the same object. Of course, when you change one, you change the other, as they are actually the same and only.

Primitive types (boolean, int, long, double, float, char, byte) are immutable: you cannot change them, you must assign them anew. Therefore, what you actually do is changing one of your references without touching the other.

Don't panic :) Your confusion is created by how Java stores values in memory.

Maybe this book could help you: http://pkris001.freeshell.org/OCA.pdf Your problem is descriped on page 83 (pdf-page 116)

In short: Java differs in storing objects and primitive datatype values.

The memory which is allocated for your running program is split into two sections. The HEAP-memory and STACK-memory.

Only the HEAP-memory stores the concrete objects and it nothing else.

new MyClassX();  // this object exists in the HEAP

the STACK-memory is used to hold the references of these created objects.

MyClassX refVar = new MyClassX(); // refVar holds one reference to the memory-address of the new MyClassX object.

For primitive datatype values its a bit different. primitive datatypes (boolean, byte, short, int, long, char, float, double, ...) are stored directly in the STACK-memory section.

int primVal = 1; // primVal stores the concrete value not a reference of 1;

So what happend when you copy...

primitve datatypes:

int primValOne = 1; // primVal stores the concrete value 1;

int primValTwo = primValOne; // primValTwo is a primitive datatype so it stores the concrete value 1, NOT a reference of primValOne

objects:

MyClassX refValOne = new MyClassX(); // refValOne stores a reference to a HEAP-memory position

MYClassX refValTwo = refValOne; // refValTwo stores the same reference like refValOne now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM