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 value

 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
marijana schumann
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this helps!
I understand now
Thanks, Chung!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic