The return type of the method in the java.awt dimension class

I am surprised to see that the getters of the height and width members have a return type double , although they are int . Moreover, the 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 review the Dimension class. Above the comment says that values ​​cannot go beyond Integer.MAX_VALUE. What for? Why do we have double between? Is there any subtle reason? Can anyone explain this to me? Sorry for my persistence!

+8
java double dimension awt
source share
3 answers

java.awt.Dimension been modified to be installed in the java.awt.geom package so that it can be used wherever Dimension2D is required. An interface for later floating point trades, so Dimension should also be. Limited to int fields, only a subset of double can be represented. Dimension2D.Float also limited.

+3
source share

The class stores height and width as an int , it just provides a method that takes a double so you can call it with double values ​​(but they immediately throw themselves into an int). There are other setSize() methods in this file that take int values ​​or even a Dimension object.

And since these values ​​are stored as int , of course, their maximum value is Integer.MAX_VALUE .

+3
source share

You can use java Dimension class with ints. If you need the Dimension class with double width and height, you can use the following:

 public class DoubleDimension { double width, height; public DoubleDimension(double width, double height) { super(); this.width = width; this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } @Override public String toString() { return "DoubleDimension [width=" + width + ", height=" + height + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(height); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(width); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; DoubleDimension other = (DoubleDimension) obj; if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height)) return false; if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width)) return false; return true; } } 
0
source share

All Articles