# Thread: Assignment of values to variables

1. Member
Join Date
Feb 2017
Posts
24
Rep Power
0

## Assignment of values to variables

Here is my code:

Java Code:
```import java.awt.Point;

public class ReferencesTest {

public static void main (String args[]) {
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1;

System.out.println("Point 1 (X,Y) is equal to " + pt1.x + " X and " + pt1.y + " Y");

pt1.x = 200;
pt1.y = 200;
System.out.println("I just doubled the values for Point 1");
System.out.println("Point 2 (X,Y) is equal to " + pt2.x + " X and " + pt2.y + " Y");

System.out.println("Let me see if I can change the values of Point 2 directly!");

pt2.x = 300;
pt2.y = 300;
System.out.println("Point 1 (X,Y) is equal to " + pt1.x + " X and " + pt1.y + " Y");
System.out.println("Point 2 (X,Y) is equal to " + pt2.x + " X and " + pt2.y + " Y");
}
}```
Here is the output:

Point 1 (X,Y) is equal to 100 X and 100 Y
I just doubled the values for Point 1
Point 2 (X,Y) is equal to 200 X and 200 Y
Let me see if I can change the values of Point 2 directly!
Point 1 (X,Y) is equal to 300 X and 300 Y

In the other programming language I know rather well (FORTRAN), I should be able to assign a value to pt2.x and pt2.y
and not have them change the values of pt1.x and pt1.y. THis does have implications for what I do inside loops, etc.

Am I missing a subtlety of the language or have I just done something silly that is wasting forum time?

Thanks...

2. ## Re: Assignment of values to variables

This principle works for primitives ( int a = b = 42), but not for objects. Both variables point to the same object.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Assignment of values to variables

This is an important concept as you go more into Java. Especially when comparing objects.

If two object references point to the same instance, they are of course equal.

Java Code:
```Point pt1, pt2;
pt1 = new Point(10,20);
pt2 = pt1;
System.out.println(pt1 == pt2); // prints true```
However,
Java Code:
```pt2 = new Point(10,20);
System.out.println(pt1 == pt2);// prints false```
But in the latter case, the Points are considered equal because pt1.x == pt2.x and pt1.y == pt2.y.

The rules for determining object equality as described above is up to the author of the class. To be able to test for
object equality, classes must override the equals() method. And usually overriding hashCode() is a good idea too.

This is a concept that you should learn quickly as it is extremely important to understand the difference. It will come
up often when writing code.

EDIT: One more notion. Please don't be misled by the Point implementation (the fact that you can directly access x and y).
This class was written (like the Dimension class) before implementation details were considered important. Both of those classes
should probably have been made immutable and employed getters to retrieve the values.

Regards,
Jim
Last edited by jim829; 03-10-2017 at 05:38 PM.

4. Member
Join Date
Feb 2017
Posts
24
Rep Power
0

## Re: Assignment of values to variables

Thanks to Surfman and Jim.

I did not respond as I was trying to wrap my brain about what you wrote.
I think I do understand it now.

Thanks for your patience in explaining this to me.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•