Results 1 to 4 of 4
  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default 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

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

    Default 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.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2015, 10:10 PM
  2. Replies: 2
    Last Post: 02-04-2013, 10:47 PM
  3. reading values into variables using java
    By kskgupta in forum New To Java
    Replies: 2
    Last Post: 05-16-2010, 05:48 AM
  4. [SOLVED] Debug: values not shown for variables
    By jwilley44 in forum Eclipse
    Replies: 7
    Last Post: 01-30-2009, 03:33 AM
  5. Replies: 2
    Last Post: 08-03-2007, 04:06 PM

Posting Permissions

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