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.

Source

line 1
Line that just executed Next line to execute
Sign in to share

Call stack

1
main()
line 1
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

Or open the Java visualizer and paste your own code.