aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pass by value" Watch "pass by value" New topic
Author

pass by value

Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
In Java all parameters are passed by value.
if I pass primitive data types to a method, then a copy of the value is sent and any changes made to the variable, will not affect the orginal value.
But if I pass object reference values, any changes made to the variable by the method, will reflect changes in the original variable. but this doesn't seem to be the case in the code below, where I pass a String object to the method.


Amir
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Good question ... but!
the aName doesnt point (refrence) any longer the same Object (String) as the one you passed to the method. Strings are immatuble (cant be changed), and you are saying: make this aName point (refrence) a NEW string which is (oldname+newname). It no longer refrences the String you passed to the method so it cant change it.
in this line: aName = aName + "newName";
you make the refrence variable refer to a completly NEW object which isnt the String you passed to the method.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Ok, thanks for your answer.
but if you read the following thread, then the answer is that the String should always return the same value, and the question about Thread and run() method is totally irrelevant.
http://www.coderanch.com/t/238400/java-programmer-SCJP/certification/Thread
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Basically your statement
aName = aName + "newName";
does not do what you think it does.
aName and name has same data before the above statement is executed, namely they contain the same address reference to the string "oldName". Now, when the above statement is executed what happen is that JVM create the string "newName" in its one and only string pool, then create another string "oldNamenewName" and put that into the string pool as well. Then, what aName is assigned is the address reference for the string "oldNamenewName". I think you were thinking that your statement would have appendthe string "newName" to the string "oldName" and thus modify the old string variable name. This append is done if you have StringBuffer type variable, not String type variable.
Does this help?


Let us be showered in the light of confusion!
marijana schumann
Greenhorn

Joined: Jun 28, 2002
Posts: 3
if you read the following thread, then the answer is that the String should always return the same value, and the question about Thread and run() method is totally irrelevant.
http://www.coderanch.com/t/238400/java-programmer-SCJP/certification/Thread

The answers above about Strings are OK, but I still don't understand how the opposite can happen in this example with Thread and run()...
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
My guess is that the answer 4 is just simply wrong. even if the run method starts before the println statement, the sName will still be the same and not be appended with 0 1 2 3 4..etc.
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Originally posted by marijana schumann:

The answers above about Strings are OK, but I still don't understand how the opposite can happen in this example with Thread and run()...

because the example with Thread and run() has the string as static member. Following section is the part that is important:

notice sName is declared as static. This means that there is only one copy of sName that is attached to the class Tux. And, in the run() method there was no sName variable, thus the sName mentioned in it is interpreted as the class member sName. As such, when strings are concatenated the result is stored into the class member sName. Where as the piggy method has a local variable name that is same as the class member sName and so the operation is done to the local variable, not the class variable sName. This is why " wiggy" won't be printed out as it is appended to a local variable, where as " 0 1 2 3" might be appended depending on when the threads are done with their execution.
Hope this help
marijana schumann
Greenhorn

Joined: Jun 28, 2002
Posts: 3
Yes, this helps!
I understand now
Thanks, Chung!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by value