The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Java
What does it mean for a variable to refer to an object?
Why it exists — An object can be large because it has many fields, so if you copy the whole object every time you assign it to a variable or pass it to a method, it costs time and memory, and a change made on one side is not reflected in the other copy. A reference makes a variable hold a value that points to the object instead of the object itself, so that when you assign or pass it, only this value is copied and several variables share one same object.
Where it's used today — When you pass an object to a method, the reference is copied and passed, so if that method changes a field of the object, the caller sees the changed value too. So where the received object must not be changed, you create a new object and move the values into it, or you use an object like String whose contents do not change once it is made. == only checks whether two variables point to the same object, so to compare whether two Strings have the same characters, you use equals.
Card is a class made as an example for this lab. In Card a = new Card();, what is inside variable a is not the Card object itself but a value that refers to that object. This value is called a reference. So when you assign a to another variable, as in Card b = a;, the object is not copied. Only the reference is copied, and a and b come to refer to the same object. If you change the contents of the object through one of them, it is changed when you look through the other too. A variable of a primitive type such as int is different. The value itself is inside it, so int b = a; copies the value. In this lab you run the same lines while changing what is assigned to b: a reference copied from a, a reference to an object newly created with new, a reference to an object newly created and then given only the owner copied over, and an int value. You can see what happens to a after b is changed, and how many Card objects were created.
Checked against real execution · Four combinations were compiled with javax.tools on JDK 21, run, and checked against the results
class Card {
String owner;
}
public class Main {
public static void main(String[] args) {
Card a = new Card();
a.owner = "Alice";
// the reference in a is copied to b, so b refers to that same object. No new object is created
Card b = a;
b.owner = "Bob";
System.out.println(a.owner);
System.out.println(a == b);
}
}
The runtime environment is JDK 21.0.12.1 (javac 21). All four combinations were compiled with javax.tools from the same source as the code on the screen, loaded with a new class loader, and Main.main was run, and the results were checked against the captured standard output. In the source of the Probe (the checking program), only lines that count Card objects (an increment of a static variable and in the constructor, and a few output lines) were added, and for every combination it was confirmed that, with those lines removed, the text is identical to the code on the screen line by line. What was checked is the two output lines, the number of Card objects created, and the facts that the step sentences rely on. The facts are the number of objects right after the line Card b = ...; ran (for the combination that assigns new Card() and then copies the owner, right after b.owner = a.owner; ran), the value of a == b, the values of the owner of the objects that the two variables refer to, and whether those two values are the same. The documentation was checked by opening the Java Language Specification (Java SE 21) directly. That there are reference values that refer to objects, that when two variables refer to the same object a state changed through one variable can be read through the other, and that an int value is copied on assignment so that the two are separate follow sections 4.2 and 4.3.1 (example 4.3.1-2); that a variable of a primitive type holds a value of that primitive type, and a variable of a class type holds null or a reference to an object of that class, follows sections 4.12.1 and 4.12.2; and that == on references is true when both are null or refer to the same object follows section 15.21.3. That == on int variables compares values (section 15.21.1) was not confirmed by opening the documentation, and was confirmed only by the result of this run. The screen draws the texts of owner, Alice and Bob, as if they were values inside the object, but owner is also a reference that refers to a String object. This lab does not cover that difference, and it does not cover how string constants come to refer to the same object. equals, which asks whether the contents of objects are the same, is covered in "In-depth lab: == and equals". Where variables and objects are placed in memory is not something this lab covers or checked. What this lab showed is the behavior that the value stored in a variable refers to an object, and that assigning copies that value.
Previous lab: Classes and objects · Next lab: null
Struck-through text was removed from the first version of the code; highlighted text was added.