AP® Computer Science A glossary
Reference equality (== versus equals)
Also called: aliases, comparing object references
Course: AP® Computer Science A Shows up in: Unit 2: Selection and Iteration
Using == or != on object references checks whether they refer to the same object. A class's equals method checks whether two objects are equivalent.
Appears in the course and exam description (CED): 2.2.A.12.6.B.12.6.B.22.6.B.3
Two variables that hold references to the same object are aliases, and == is true for them. Compare a reference with null to check whether it refers to an object. Compare String contents with equals.
String a = new String("goal");
String b = new String("goal");
System.out.println(a == b);
System.out.println(a.equals(b));
Output:
false
true Learn it properly, with a checkpoint 2.6 Comparing Boolean Expressions