public class Box {
private int v;
public Box(int x) { v = x; }
public boolean equals(Object o) {
if (!(o instanceof Box)) return false;
return v == ((Box) o).v;
}
}
// Usage:
Box b = new Box(3);
System.out.print(b.equals(null));What is printed?
- Acheck_circle
false
- B
true
- C
NullPointerException
- D
Compile error
Explanation
null instanceof anything is false, so the early return triggers and equals returns false. This is the safe pattern for handling null.