Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
P a = new P(1, 2);
P b = new P(1, 2);
System.out.println(a.equals(b) + " " + a);
}
}
record P(int x, int y) {}true P[x=1, y=2]
- Expecting
false: a record'sequalscompares its components one by one, so two records with the same data are equal, even though==givesfalse. - Expecting the program not to compile: the compiler itself writes a record's constructor, accessors,
equals,hashCodeandtoString.
- What does
a == bprint for these two records, and why? - How do you get a point like
a, but withxequal to 5?