Rectangle class functions getX (), getY (), etc. Come back in double precision

Well, I know what they do according to my experience and the Oracle Java API documentation, but I wonder why. Through the constructor, I am allowed to pass arguments of type int to the Rectangle class, the internal representation of the data x, y, etc. It is of type int , and also setSize() only with the exception of arguments of type int . But why all methods, such as getX() , getY() , getWidth() , etc., return double when there can be no precision? Why not a simple int as expected?

EDIT: I understand that it is derived from the Rectangle2D class, but this is still not justification for simply not providing any int - getX() and getY() functions, as opposed to Point and Point2D These methods are not abstract. In addition, setLocation() also not abstract.

+7
source share
2 answers

I believe because it extends Rectangle2D , which is used in Graphics2D , and it says:

This class is just an abstract superclass for all objects that store a 2D rectangle. The actual presentation of the storage data remains in the subclass.

So, if any of its subclasses represents dimensions in double , it might need to be compatible with each other. If you are reading Javadoc for Rectangle # getX () :

Returns the X coordinate of the bounding box in double precision.

It is indicated:

public abstract double getX () in the RectangularShape class.

+3
source

I think they set them to search:
If some APIs require an int version of the x or y coordinate, it can directly use recObject.x or recObject.y, respectively.
If any API needs an x ​​or y double coordinate version, then it can use getX () and getY ()

0
source

All Articles