Why does java.awt.Point provide methods to set and get doubles but store x and y as int's?

感情迁移 提交于 2019-12-08 19:26:46

问题


As you can see in the Oracle Documentation for java.awt.Point, x and y are stored as int. However, getX and getY return double. While there is a setLocation method that takes 2 double types, there is no constructor that does. Furthermore, the double gets truncated to an int internally anyway.

Is there a good reason for this? You might avoid a cast on setLocation by having a method that takes double types, but you have the opposite problem when you call getX and getY. There's also a misrepresentation of the precision of the Point class by returning a double from getX and getY.


回答1:


Because it extends from java.awt.geom.Point2D which is used in Graphics2D abstract class. It was implemented this way for compatibility reason since before this, java only supported java.awt.Graphics where all methods are int based.



来源:https://stackoverflow.com/questions/9569918/why-does-java-awt-point-provide-methods-to-set-and-get-doubles-but-store-x-and-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!