References, equality, identity
An object variable is a reference: null, aliases, identity (==) versus equality (equals), a redefined equals, shallow or deep copy, in Java.
- 27 cards
- 4 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Understand
A few cards from the deck
Reference: what is it?
Show the answer
The value stored in an object variable, which tells where the object sits in memory.
Point b = a;copies the reference, not the object:aandbthen designate the same point.What are
a.xandc.xat the end of this program?
```java
Point a = new Point(1, 2);
Point b = a;
Point c = new Point(1, 2);
b.x = 5;`5and11and15and51and5
Show the answer
5and1bis an alias ofa: the same object, soa.xbecomes 5.cis a second object created with the same values: it does not change.A variable of object type does not hold the object itself but a … to it.
Show the answer
A variable of object type does not hold the object itself but a reference to it.
Primitive types (
int,boolean…) store their value directly; all the others go through a reference.null: what is it?
Show the answer
The reference value that designates no object.
Reading an attribute or calling a method through
nullraisesNullPointerException.Why is
a.equals(c)false whenPointhas not redefinedequals, even ifaandchave the same coordinates?equalsonly applies to strings- The
equalsofObjectcompares references, like== - The coordinates are
ints, whichequalscannot read - Two separately created objects are never equal in Java
Show the answer
The
equalsofObjectcompares references, like==The class must say what equality of two points means. Once
equals(Object o)is redefined to comparexandy,a.equals(c)becomes true, whilea == cstays false.