Rectangle class functions getX(), getY(), etc. return in double precision
Well I know they do, according to my experience and the Oracle Java API documentation, but I wonder why. Through the constructor I'm only allowed to pass int
type arguments to the Rectangle
class, the internal data representation of x, y, etc. are of the type int
and also setSize()
only excepts arguments of the type int
. But why do all methods like getX()
, getY()
, getWidth()
, etc. return a double when there can not be any precision? Why not simple int
s as expected?
EDIT: I do understand that it is derived from the Rectangle2D
class, but that is still no reason to just simply not provide any int
-based getX()
and getY()
functions, as in difference with the Point
and Point2D
class those methods are not abstract. Also setLocation()
is not abstract either.
I believe because it extends Rectangle2D which is used in Graphics2D, and it says :
This class is only the abstract superclass for all objects that store a 2D rectangle. The actual storage representation of the coordinates is left to the subclass.
So if any of its subclass represents the dimensions in double
should be compatible with one another, perhaps. If you read the Javadoc for Rectangle#getX():
Returns the X coordinate of the bounding Rectangle in double precision.
Specified by:
public abstract double getX() in class RectangularShape.
I think they put them for perpose:
If some API needs the int
version of the x or y coordinate then it can directly use recObject.x or recObject.y respectively.
If some API needs the double
version of the x or y coordinate then it can use getX() and getY()
上一篇: 如何正确处理异步数据库复制?