6

I am confused to as why the Java Point class takes in two int paramaters and the getX() and getY() methods return doubles. For example I could define a Point

Point p = new Point(4,6);

If I were to call..

p.getX();

It would return 4.0. and if I were to call

p.x;

I would get 4.

Any reason for this?

James Fazio
  • 6,370
  • 9
  • 38
  • 47
  • 3
    Mostly, it's because of backwards compatibility and the fact that `Point` has been hacked together over a relatively long period. – Louis Wasserman Apr 19 '12 at 03:42

1 Answers1

3

There are Point2D.Double and Point2D.Float classes that extend Point2D which is a superclass of Point and they need to be able to work with floating point values. Note that there is also a setLocation( double, double ).

Point2D is an abstract class that implements the distance calculation for points, and setLocation, getX, and getY are its abstract methods, which is why they all use doubles and why Point has to implement them with doubles in the signature.

trutheality
  • 23,114
  • 6
  • 54
  • 68
  • Strange how you must initialize both x and y to ints, but then can set the location of x and y to doubles. – James Fazio Apr 19 '12 at 03:47
  • `setLocation` in the `Point` class will round it to the nearest integers, so a `Point` always has integer coordinates. – trutheality Apr 19 '12 at 03:49