public class P {
private int x;
public P(int x) { this.x = x; }
public boolean equals(Object o) {
if (!(o instanceof P)) return false;
return ((P) o).x == this.x;
}
}
// ...
P a = new P(3);
String s = "3";
System.out.println(a.equals(s));What is printed?
- Acheck_circle
false
- B
true
- C
Compile-time error
- D
ClassCastException
Explanation
Since s is not an instance of P, equals returns false at the first check.