Java objects and references, visualized
A variable that holds an object holds a reference to it, not the object itself. Step through the program and watch two variables share one object while a third has its own copy.
Call stack
1
main()
Step 1/1
What to watch
- Point b = a copies the reference, so a and b point to the same object.
- moveRight changes the object through its parameter, and the change shows up through a as well.
- a == c is false even though the values match, because == compares references, not contents.
Try changing it
- Set b to a new Point inside moveRight and see whether a changes.
- Add an equals method to Point and compare a.equals(c).
Edit the code above and press Visualize to see your version run.
More Java examples
- Java recursion visualizer
- Bubble sort in Java, step by step
- Binary search in Java, step by step
- Java ArrayList visualizer
- Java linked list visualizer
- Java 2D array visualizer
Or open the Java visualizer and paste your own code.