Understanding object references is an important part of AP Computer Science A. In this interactive practice set, you will work with Topics 1.12–1.14: object creation, object references, using object references, and calling instance methods.
Key Concepts to Remember Before You Start:
References vs. Primitive Values:
Primitive variables store values such as integers and doubles. An object
variable stores a reference to an object. The reference can
be used to access the object’s instance variables and call its instance methods.
Aliasing:
When two reference variables refer to the same object, they are aliases.
A change to that object’s state through one reference can be observed through
the other reference.
null References:
A reference variable containingnulldoes not currently refer
to an object. Calling an instance method using anullreference
causes aNullPointerExceptionat runtime.
Object Creation:
Thenewkeyword creates an object by invoking a constructor.
The resulting reference can be assigned to an object variable.
Reference Equality:
For object references, the==operator determines whether two
references refer to the same object. It does not compare the objects’
instance-variable values.
Test your object-reference and memory-tracing skills with these 10 practice questions. Select an answer to see the explanation and understand the reasoning behind each result.
Topics 1.12–1.14 Practice
String s2 = s1;
s2 = “Python”;
System.out.println(s1);
s2 receives the same reference value as
s1. Both variables refer to the String containing
"Java".When s2 = "Python"; executes, only s2
is reassigned. The value of s1 remains unchanged, so
s1 still refers to "Java".Car c2 = c1;
c1.setColor(“Blue”);
System.out.println(c2.getColor());
setColor changes the car’s color instance
variable, what is printed?c1 and c2 refer to the same
Car object. This is called aliasing.When c1.setColor("Blue") changes the object’s state,
that same changed state is visible when the object is accessed through
c2.null?NullPointerException occurs at runtime when code
attempts to use a reference whose value is null in a
context that requires an object.null when no other value is assigned.This is different from local variables, which must be explicitly
initialized before they are used.System.out.println(str.length());
str contains null, so it does not refer to
a String object. Calling length() requires a String object,
resulting in a NullPointerException at runtime.Person object and assigns its reference to a variable
named p?new keyword creates an object and invokes the
appropriate constructor.The general form is:ClassName variableName = new ClassName(arguments);Rectangle r2 = new Rectangle(10, 5);
Rectangle r3 = r1;
System.out.println((r1 == r2) + ” ” + (r1 == r3));
new expressions create two separate
Rectangle objects.Therefore, r1 == r2 is false.Then:Rectangle r3 = r1;copies the reference value from
r1 into r3.
Both variables therefore refer to the same object, making
r1 == r3 true.public void modifyPoint(Point p).
If this method changes the coordinates of p internally,
what happens to the original Point object passed to the method?Topics 1.12–1.14 Practice Complete!
Understanding Object References and Aliasing
Aliasing is an important concept in AP CSA. When two reference variables refer to the same object, modifying that object’s state through one reference can affect what is observed through the other reference.
When tracing Java code, pay close attention to whether a statement creates a
new object with new or simply copies an existing reference value.
Continue Your AP CSA Practice
You can continue reviewing the remaining Unit 1 object concepts with Topic 1.15: String Methods .
For the complete current AP CSA framework, visit the AP CSA Unit 1: Using Objects and Methods practice hub.