3 ms·
It's a bit of an unkind example as most would write the latter as if (obj instanceof Point p) { System.out.println(p.x()+p.y()); } But I can m
by morepork 3y ago
It's a bit of an unkind example as most would write the latter as
if (obj instanceof Point p) {
System.out.println(p.x()+p.y());
}
But I can maybe see the value if you need to use x/y more than once.
However in Java it's common to have getX() style accessors, for which this syntax doesn't seem that helpful as your variable would end up being called getX
- valenterry 3y agoI think this syntax for accessing x and y only works for java records anyways and not for methods.