2
public class Practice {
    public int value;
    public Practice child;
    public Practice(int value) {
        this.value = value; 
    }

    public static void main(String[] args) {
        Practice obj = new Practice(10);
        Practice obj2 = new Practice(9);
        Practice obj3 = new Practice(11);

        obj.child = obj3;
        obj.child = obj2;

        System.out.println(obj2 == obj3);


    }

}

In this code, since I made obj.child = obj3, then I thought you could substitute obj3 for obj.child so that in other words the line "obj.child = obj2" could mean "obj3 = obj2". However, in the print statement it returns false.

However, if you do something like

obj.child = obj3;
obj.child.value = obj2.value;

Then you are actually manipulating obj3 as its value changes, however in the previous code obj3 is only "replaced" however the object remains unchanged.

Same thing here:

obj.child = obj3;
obj2 = obj.child;

Now they are actual equal... so you can subsitute obj3 for obj.child? Now Im confused... Bit of a novice coder so I would appreciate if someone would clear this up for me. Thank you!!

Andrew
  • 21
  • 2
  • 3
    Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – knittl Apr 15 '23 at 15:49
  • `var x = 4; var y = x; y = 2;` – x is still 4. – knittl Apr 15 '23 at 15:50
  • 1
    It sounds like you're reading the `=` as mathematical equality, so that `x=y, x=z` means `y=z`. It's actually assignment, in math sometimes written `:=`, and means something like "forget the previous definition of x and use this new one instead" – that other guy Apr 15 '23 at 16:00
  • I suggest you try stepping through your program with a visualisation like [this one](https://pythontutor.com/java.html). – kaya3 Apr 15 '23 at 16:15
  • @knittl The pass-by-value thing is for passing objects into methods. This example however is all in the main method. Basically i'm saying why obj.child = obj3 is different from saying obj2 = obj3, assuming that obj.child is obj2 – Andrew Apr 15 '23 at 17:31

1 Answers1

1

This is what happens after each of your code lines.

1

Practice obj = new Practice(10);
// HEAP
// ====
//                +--------------+
//      obj ----> + Practice(10) +
//                +--------------+

2

Practice obj2 = new Practice(9);
// HEAP
//                +--------------+
//      obj ----> + Practice(10) +
//                +--------------+
//
//                 +-------------+
//      obj2 ----> + Practice(9) +  
//                 +-------------+   

3

Practice obj = new Practice(10);
// HEAP
// ====
//                +--------------+
//      obj ----> + Practice(10) +
//                +--------------+
//
//                 +-------------+
//      obj2 ----> + Practice(9) +  
//                 +-------------+        
//  
//                 +--------------+       
//      obj3 ----> + Practice(11) +
//                 +--------------+    

4

obj.child = obj3;
// HEAP
// ====
//                +--------------+
//      obj ----> + Practice(10) +
//                +    child -------------+
//                +--------------+        |
//                                        |
//                 +-------------+        |
//      obj2 ----> + Practice(9) +        |
//                 +-------------+        | 
//                                        |
//                 +--------------+       |
//      obj3 ----> + Practice(11) +  <----+
//                 +--------------+ 

5

obj.child = obj2;
// HEAP
// ====
//                +--------------+
//      obj ----> + Practice(10) +
//                +    child -------------+
//                +--------------+        |
//                                        |
//                 +-------------+        |
//      obj2 ----> + Practice(9) + <------+
//                 +-------------+         
//                                        
//                 +--------------+       
//      obj3 ----> + Practice(11) +
//                 +--------------+ 
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Makes sense, so obj.child stores a pointer and not the actual object? But how is saying obj.child.value different from saying obj2.value? – Andrew Apr 15 '23 at 16:16
  • In the last example (line 5), they are not different. Running your code will prove it to you. `obj.child.value == obj2.value` will return true. – Yassin Hajaj Apr 15 '23 at 16:26