Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass By reference Doubt

 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

I am stuck with Pass by reference,Here is the my code



Output:



In this code,by the context of pass by reference we have change the name from Ram to raju.Its takes the effect.But after that i have assign null to the reference,thats not makes effect.
that means that doen't make object as null.Why?


Please clarify my doubt.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The s referred to in the changeValue method is made to point to a null location and not the s1 referred to in the main. When the changeValue method begins to execute- both s and s1 would be referring to the same object (that explains the reason why the name changed), but when you try to reference s to a null, only s points to a null and not the s1. s1 still continues to refer to the valid object on the heap.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no pass-by-reference in Java. It passes references by value.

See CallByReferenceVsCallByValue for more detail.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:There is no pass-by-reference in Java. It passes references by value.

See CallByReferenceVsCallByValue for more detail.

Oh yeah, how did I miss this point!
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Both of you ,

Thanks for your reply.It makes me clear.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,
At first this statement 'pass-by-reference' gave me serious headache till I spent a whole day writing codes to understand what was going on. If I my voice could be heard I would have suggested that the statement should be changed from 'pass-by-reference' to 'pass-by-reference-values'.

However...@Mohamed take note that if you write this same program to change a primitive type, -unlike objects- the value does NOT change

Something 'similar' could pop up in the real exams

Take NOTE of the following modification to your program:


Output:
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Munees!...Its good to see you here ...once again we are putting into "PRACTICE" our thoughts.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ikpefua Jacob-Obinyan wrote:
However...@Mohamed take note that if you write this same program to change a primitive type, -unlike objects- the value does NOT change

That's why it is called pass by values. And the values are copied- in case of references- the location of the instance (that is the value the references hold) and in case of primitives the value of the primitive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic