This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object References explanation  RSS feed

 
Dj Galla
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book on JAVA, and trying to understand the way Object References work.  I am used to PHP, so the results below are VERY confusing to me. I was wondering if someone could explain it in an easy way for me to grasp.

Looking at the code below (I have 5 separate examples), I would assume the two designated variables (like pt1 and pt2, or x and y) to always output different results, but they don't.   In PHP, if I use X = Y, and THEN change the value of Y, X doesn't change.



Please let me know. I guess I'm trying to wrap my head around this. Maybe looking for a metaphor, or some way to explain why things work this way.
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, object references can be thought of as little fingers hanging out inside the JVM pointing to things.
Or at least that's what my old mentor used to visualize them as.


If you change the object pt1 is pointing to, e.g. by doing
then the change will be reflected in the object pt2 is pointing at.

But if you change what pt1 is pointing to, e.g. by doing
then pt1 and pt2 will no longer be pointing to the same thing.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dj Jabbamonkey wrote:
Please let me know. I guess I'm trying to wrap my head around this.


This line doesn't create any objects. It merely declares two reference variables. Reference variables are used to point to objects, but at this point, they are not pointing to (referring to) any object yet.

Henry
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(can't seem to figure out how to edit a post)

Edit to previous post: "If you change the object pt1 is pointing to..." could be worded better as "If you modify/mutate the object pt1 is pointing to..."
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say p2=p1, you aren't creating any new object; you are just saying that p2 references the same thing as p1 does. If you then say p2.x=10, then p1.x is now 10, since they refer to the same object.

The same thing happens if you say p1.x=33. Now p1.x is also 33.

If you want a new object that originally has the same content as p1, you use either of these:

Now p2 is a separate object, and setting p1.x has no effect on p2, and vise versa.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Example 1
  • Please refer diagram for this.

  • Example 2
  • Creates two new objects of Point and assigns instance variables of both objects x = 100 and y = 100.
  • Both objects are referred by reference varaibles pt3 and pt4 so both refer to different objects.
  • Means now we have pt3.x = 100, pt3.y = 100 and pt4.x = 100 and pt4.y = 100


  • Assigns instance variables x = 200 and y = 200 of object only referred by reference variable pt3.
  • Means now we have pt3.x = 200, pt3.y = 200 but pt4.x = 100 and pt4.y = 100
  • So prints different output


  • Example 3
  • This is just declaration as above mentioned. Object of Point is not created yet.


  • It is same like example 1.
  • Means only one object of Point is created and referred by two reference variables here pt5 and pt6.


  • Example 4
  • This is not object, It is declaration of local variables of primitive data type int, named x and y.


  • Assigns value 1 to x i.e. x = 1 and then y = x means y also becomes 1
  • Now we have x = 1 and y = 1


  • Assigns x = 5.
  • Now we have x = 5 and y = 1


  • Pay attention and observe, you tell me why printing same output?


  • Example 5
  • If you solve my question of example 4, you will solve this also 
  • ObjectReference.png
    [Thumbnail for ObjectReference.png]
     
    Dj Galla
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok. I think I got it. Thanks for the detailed description by everyone.

    So (in example 1) pt1 and p2 are just variables of type Point, and the objects are the x and y coordinates in the Point. p1 and p2 are pointing to the same x and y coordinates. I think my biggest confusion was with example 5, where I accidentally had x in place of y in the println, and x was equal to y ... so, the problem there was user error.  Oops.

    Thansk again
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dj Galla wrote:So (in example 1) pt1 and p2 are just variables of type Point, and the objects are the x and y coordinates in the Point. p1 and p2 are pointing to the same x and y coordinates.
    No no x and y are not objects. Object is the blue circle with assumed address location 1000. That has instance variables x and y in it. x and y are not static variables so belongs to instance i.e. an object of Point so called instance variables means each object of Point we create will have their own x and y.
    So (in example 1) pt1 and p2 are just variables of type Point, 
    Yes pt1 and pt2 are variables of type Point. They hold reference of object of Point class so called reference variables. Keep in mind in example 1 only one object is created and by the end referred by two reference variables pt1 and pt2.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    and these instance variables x and y are of int type.
     
    Dj Galla
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:Object is the blue circle with assumed address location 1000.


    Where does this 1000 come from? You refer to it when x=100 and y=100 ... but also when x=200 and y=200?

    I understand the object creation (i.e. Point()).
    And the instance variables, which are part of the object (both x and y).
    If they were static variables, they would be outside of the object. So not Point(x,y) ... but int x and int y (with no object association).
    Correct?
    So, when pt1 = new Point(100, 100); ... then it is REFERRING the this new object created. It is not BECOMING the object. So when pt1=pt2, they both REFER to this object.
    Correct?
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dj Galla wrote:Where does this 1000 come from?
  • It is just assumed to understand which object we are talking about by saying like object which is at location 1000.
  • Here I drew three blue circles to make you understand that how values of it's x and y changes in exececution of statements(See steps in red circle) but pay attention all those diagram have only one address i.e. 1000 means there is only one object.

  • You refer to it when x=100 and y=100 ... but also when x=200 and y=200?
  • If you carefully observe step no 2  (Step no is a digit in red circle at left side corner in image), It creates an object of Point which calls parameterized constructor which initializes x = 100 and y = 100.
  • Now see step no 4, where we assigned 200 as new value to x and y of object referred by pt1 i.e. same object at location 1000 which is also referred by pt2 because we assigned it, see step 3.

  • I understand the object creation (i.e. Point()).
    And the instance variables, which are part of the object (both x and y).
  • Yes x and y are instance variables of an object of Point.


  • If they were static variables, they would be outside of the object. So not Point(x,y) ... but int x and int y (with no object association).
    Correct? 
  • Yes If those are static variables (aka class variables) will belong to class i.e. Here Point. There will be only one x and one y and shared among the objects of Point.


  • So, when pt1 = new Point(100, 100); ... then it is REFERRING the this new object created
  • Yes it creates new object of Point which will have instance variable x = 100 and y = 100, see step no 2.
  • Yes at this point reference variable pt1 refers to this newly created object. In diagram I this object assumed location 1000 so we can understand which object pt1 refers to.

  • It is not BECOMING the object
  • I didn't get what do you mean by this?


  • So when pt1=pt2, they both REFER to this object.
    Correct?
  • Not pt1 = pt2 but pt2 = pt1, Yes both are referring to the same object i.e. which is at location 1000, because here at step no 3 we assigned pt2 = pt1 which makes pt2 refer the same object which was referred by pt1.
  • So when you print values of x and y of object referred by pt1 and pt2 prints same output. Why? because both are referring to the same object i.e. object at location 1000

  • Hope you understand
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!