• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pass by value on reference (pass a copy of the reference)

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

i'm trying to clarify on my head this pass by value when we are talking about reference variables.
i have this example:




result: [20][]

now, in my head, after processData is called,
the reference s1 is passed to the method and because st1 is pointing to  s1 object we will get s1 = 20

what i don't understand is this:
if st2 after the assignment is pointing to st1, that st1 is pointing to s1 object so that why we get value 20.
st2 it is not pointing same to the s1 object after this st2 = st1; ?

reference st2 -> st1 -> s1 ->  object ?

i would like another similar example please if someone can help me understand much clearier.

p.s i already read it this link java pass by value
and i know that it is passed a copy of the reference  

thank you,
Stefan


 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is always pass-by-value. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Banu wrote:what i don't understand is this:
if st2 after the assignment is pointing to st1, that st1 is pointing to s1 object so that why we get value 20.
st2 it is not pointing same to the s1 object after this st2 = st1; ?


Because Java uses pass-by-value (as you already know), variables st1 and st2 are local variables in the processData() method. So after executing the statement st2 = st1;, both variables st1 and st2 are referring to the Stack instance created on line1 (and referred to by s1). So if you would add st2.push(new Integer(40)); after that statement, the Stack instance (created on line1 and referred to by s1) will have a second element. But s2 will not refer to the Stack instance created on line1 (and referred to by s1), because the value of s2 (which might be the address of the Stack instance created on line2) was not changed, only the value of st2 was changed (and that was a copy of s2).

Stefan Banu wrote:i would like another similar example please if someone can help me understand much clearier.


Here's a similar example from the real world. Assume you have a very nice picture of your family on your computer and you decide to print this picture in full-colour (s2). You are really impressed with this picture and you want to make a copy, so you go to the copy center and create a copy of this picture (processData). Back home you'll put the copy on the table (st2) and your little brother (or sister/nephew/niece) draws a very big cross on this copy with a red permanent marker (st2 = st1;). Now although the copy has a red cross and is completely messed up, the original print doesn't have this red cross and still looks amazing.
In this excellent topic you'll find a great explanation (with some code examples) about pass-by-value with reference variables. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Stefan Banu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Roel De Nijs,

great explanation, i'll read also the pointed link.

thank you for your time and explanation,
Stefan
reply
    Bookmark Topic Watch Topic
  • New Topic