• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

a question about memory pointers

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the output of the this method

i think its
0
200

why its the opposite??

the answer is
200
0

i thought in a certain way
1st p,q have the (0,0) values
than we put them in some method which calls p >>a
q>>b
2nd it puts the values of (100,100) inside of a( which is p)
3rd it puts the values of a into temp
4th it puts the values of b=(0,0) inside a
5th it puts in b the values of temp which are(100,100)
so why its the opposote??
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget the semantics of Java parameter passing is pass by value. Specifically, when objects are passed to a method parameter, a copy of a reference to the object is passed. In this case:

When the tryFun() method is called:


Code examples like this can be tricky until you really get a firm grasp of pass by value semantics.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the start you want to say that
a and p
b and q

are not the same object???
so why if we change a.x=100
a.y=100
then the value of p got to be(100,100)
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case we say the "he this change in b applies also to q"??

in what change in b we say that its stays in b and not applies to q??

you said temp "refers to the same Point object that a and p refer to"

so they are linked together and every chahge in one leads to the chnge in the other

but in some point you say that its not true anymore

why???/
[ February 16, 2008: Message edited by: donaldth smithts ]
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that you swap the references inside the tryFun method doesn't mean they are swapped outside it.
  • You pass a reference to p (maybe 1324abcd) to tryFun, and then get the object at 1234abcd which you are now calling a and change its fields.
  • You also pass a reference to another object which might (but probably isn't, but we'll stick with the numbers) at 2345bcde.
  • You call it b and don't change it.
  • Then you change it around so inside the method you swap a to be 2345bcde and b to be 1234abcd.
  • But p is still 1234abcd and q is still 2345bcde.
  • Then you print out p and get 200, and q gives 0.
  • Voila!
  • Read what Garrett Rowe wrote very slowly and carefully.

    Try this change:
     
    alex lotel
    Ranch Hand
    Posts: 191
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so when we make
    a.x=100
    a.y=100

    it doesnt count as change in reference??
     
    Garrett Rowe
    Ranch Hand
    Posts: 1296
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [donaldth smithts]: so when we make
    a.x=100
    a.y=100

    it doesnt count as change in reference??


    No thats not a change in the reference. That is a change to the state of the object that the reference refers to.

    Check out Cup Size -- a story about variables.
     
    Campbell Ritchie
    Marshal
    Posts: 82459
    594
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Garrett Rowe:
    No thats not a change in the reference. That is a change to the state of the object that the reference refers to.[/URL].

    Exactly. You are not changing "a" or "b." You are changing "x" or "y."

    "a" and "b" are the object references, "x" and "y" are the fields of the object.
     
    alex lotel
    Ranch Hand
    Posts: 191
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so after we change there reference we can say that
    a and p
    b and q are detached

    if we say after that operation that
    a.x=1000
    a.y=1000
    p is still will be equaled to(100,100)
     
    reply
      Bookmark Topic Watch Topic
    • New Topic