0

I was messing the Java's Point2D.Double class and ran into some issues with changing the values of points when set equal to one-another first.

This is how Points work in Java:

/** Testing with Points */
    System.out.println("Points: ");

    // Create a new point:
    Point2D.Double point1 = new Point2D.Double(1, 1);
    System.out.println(point1);

    // Create another new point, based on the old point
    Point2D.Double point2 = point1;
    System.out.println(point2);

    // Change point1, print both again.
    point1.setLocation(0, 1);
    System.out.println(point1);
    System.out.println(point2);

The output of that code would be:

Points: Point2D.Double[1.0, 1.0]Point2D.Double[1.0, 1.0]Point2D.Double[0.0, 1.0]Point2D.Double[0.0, 1.0]

Notice point2 ends up with the value [0.0, 0.0], even though the only point changed was point1?


Here is the same code again, but with primitive integers:

/** Testing with Integers */
    System.out.println("Integers: ");

    // Create a new integer (primitive)
    int integer1 = 1;
    System.out.println(integer1);

    // Create another new integer (primitive), based on the old int.
    int integer2 = integer1;
    System.out.println(integer2);

    // Change integer1, print both again.
    integer1 = 0;
    System.out.println(integer1);
    System.out.println(integer2);

The output of this code would be:

Integers: 1101

Only the Point2D class seems to carry values from class to class like that. The Point2D documentation for the setLocation function reads:

Sets the location of this Point2D to the specified double coordinates.

Notice the word THIS


I was actually able to work around this problem with this code:

Point2D.Double point2 = new Point2D.Double(point1.x, point1.y);

but I still want to understand why the Point2D class works this way and what other classes have this same property.

Thank you for reading, I look forward to reading your responses.

user2815708
  • 193
  • 3

3 Answers3

1

Point2D.Double is a class. You create only ONE instance of that object. So by using:

Point2D.Double point2 = point1;

You are only creating a "pointer" which points to the SAME memory as the first object. In your second example you create TWO different instances of your Point object.

Please see my poorly drawn image.

Point references

HectorLector
  • 1,851
  • 1
  • 23
  • 33
0

I think you are confusing references and instances (objects). The point1 is a reference to an instance, and you make the point2 a reference to the same instance. When you modify the instance, it does not matter which reference you observe pointing to it.

In the case of new Point2D.Double(point1.x, point1.y), you are creating a new instance.

In case of ints, you are using new instances also.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
0

point1 and point2 are referencing the same object. If you change one, you change both, unless you use new as you have found.

It works fine for int because int is a primitive data type so there are no instances of it.

Read this for more on java pointers, references, and passing arguments.

http://javadude.com/articles/passbyvalue.htm

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
  • Thank you very much for answering :). In light of what you said, why wouldn't the code '/** Testing with Integers */ System.out.println("Integers: "); // Create a new integer (class) Integer int1 = new Integer(1); System.out.println(int1); // Create another new integer (class), based on the old int. Integer int2 = int1; System.out.println(int2); // Change integer1, print both again. int1 = 0; System.out.println(integer1); System.out.println(integer2);' do the same thing as the point code? Does it have to do with the fact that changing points calls a function? :) – user2815708 Jan 28 '14 at 15:35
  • I got 1101 when I ran that. You are printing integer1 and integer2 when you meant to print int1 and int2. – Dodd10x Jan 28 '14 at 16:03