Results 1 to 4 of 4
- 03-10-2017, 05:24 PM #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"); } }
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...
- 03-10-2017, 05:35 PM #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.
"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 03-10-2017, 06:29 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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
Java Code:pt2 = new Point(10,20); System.out.println(pt1 == pt2);// prints false
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,
JimLast edited by jim829; 03-10-2017 at 06:38 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-14-2017, 06:30 PM #4
Member
- Join Date
- Feb 2017
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
How to add variables and default values to arraylist
By maas in forum Advanced JavaReplies: 3Last Post: 03-23-2015, 11:10 PM -
Trying to set various values into an array using variables and a loop
By monopolyman900 in forum New To JavaReplies: 2Last Post: 02-04-2013, 11:47 PM -
reading values into variables using java
By kskgupta in forum New To JavaReplies: 2Last Post: 05-16-2010, 06:48 AM -
[SOLVED] Debug: values not shown for variables
By jwilley44 in forum EclipseReplies: 7Last Post: 01-30-2009, 04:33 AM -
how to have function variables remember their values between calls
By asterik123 in forum New To JavaReplies: 2Last Post: 08-03-2007, 05:06 PM
Bookmarks