wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass By reference Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pass By reference Doubt" Watch "Pass By reference Doubt" New topic
Author

Pass By reference Doubt

Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
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.

Cheers Munees
My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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.

Mohamed Sanaulla | My Blog
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

There is no pass-by-reference in Java. It passes references by value.

See CallByReferenceVsCallByValue for more detail.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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

Joined: Mar 19, 2010
Posts: 138
Hi Both of you ,

Thanks for your reply.It makes me clear.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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:


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hey Munees!...Its good to see you here ...once again we are putting into "PRACTICE" our thoughts.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Pass By reference Doubt
 
Similar Threads
Doubt in String class
Passing a Primitive By reference
String Doubt
A doubt in passing references