Search code examples
javadoubleawtdimension

Method return type in Dimension class of java.awt


I am surprised to see that getters of height and width members has return type double, albeit they are int. Moreover, setSize method with double parameters has the following definition:

/**
 * Sets the size of this <code>Dimension</code> object to
 * the specified width and height in double precision.
 * Note that if <code>width</code> or <code>height</code>
 * are larger than <code>Integer.MAX_VALUE</code>, they will
 * be reset to <code>Integer.MAX_VALUE</code>.
 *
 * @param width  the new width for the <code>Dimension</code> object
 * @param height the new height for the <code>Dimension</code> object
 */
public void setSize(double width, double height) {
    this.width = (int) Math.ceil(width);
    this.height = (int) Math.ceil(height);
}

Please have a look at Dimension class. Above comment says values cannot go beyond Integer.MAX_VALUE. Why? Why do we have double in between? Is there any subtle reason? Can anyone please explain this to me? Sorry for my insistence!


Solution

  • java.awt.Dimension was retrofitted to fit into the java.awt.geom package, so that it can be used wherever a Dimension2D is required. The interface for the later deals with floating point, so Dimension has to also. Being limited to int fields, only a subset of doubles can be represented. Dimension2D.Float is similarly restricted.