As described in
Why does java.awt.Point provide methods to set and get doubles but store x and y as int's? the Java class java.awt.Point does not have a get method that returns an int
. However, you can access x
and y
directly, which are both int
types.
With that said, which is the lesser of the 2 evils?
Point location = this.getLocation();
int locX = (int)location.getX();
int locY = (int)location.getY();
or
Point location = this.getLocation();
int locX = location.x;
int locY = location.y;
I try to keep the standard practice of using getters (accessor methods) when possible, but in this scenario it requires a cast. I can avoid the cast, but I must access the fields x
and y
directly. Assuming that internally getX
and getY
are simply casting the int to a double
and then I'm casting it back to an int
, it feels wrong.