pass by value

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.

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.

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?

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

posted Jun 30, 2002 11:05:00

0

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

posted Jun 30, 2002 17:31:00

0

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