Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
Box a = new Box(5);
Box b = new Box(5);
Box c = a;
System.out.println(a.equals(b) + " " + a.equals(c));
}
}
class Box { int n; Box(int n) { this.n = n; } }false true
- Expecting
true true: without an override,equalscomes fromObjectand compares references, so two boxes with the samenare not equal. - Expecting a compile error: every class inherits
equalsfromObject, so it can be called onBoxwithout writing it.
- How do you make two
Boxobjects with the samenequal? - Which method must be overridden together with
equals?