File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing variables into methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing variables into methods" Watch "Passing variables into methods" New topic
Author

Passing variables into methods

Anand Jayaraman
Ranch Hand

Joined: Jan 28, 2004
Posts: 104
Hi Guys!!

Can somebody help me out with this???



The output is:

java javaranch
317
317

I find it quite odd because, both functions changeLong() and changeString() function quite similarly (except that they opertate on different types). So if the string s1 retains its old value of "java" after going thru' with the method, why doesn't the long array a1 retain its values??

In other words, I would have expected the output to be:
java javaranch
357
317

Can someone point out what is it that I am overlooking??


Cheers!!!
Anand Jayaraman
Ranch Hand

Joined: Jan 28, 2004
Posts: 104
This is actually from K&B!



Cheers
Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19


Here what you are overlooking is the fact that Java deals differently with String object from any other object.

In Java String is "immutable".


When we say "immutable" it means once an object is created you cannot change it, thus for String, once it has created you cannot change its value.

Thus whenever code tries to change the string object, JVM creates new String object with same value and will perform operation on that.

So at the line :

s1 = s1 + "ranch"; return s1;

JVM will create new String object "java" will point s1 to that and will add the "ranch" string to get again new String object with value "javaranch" and make s1 point to this,

thus s2 will point to "javaranch" object, in the doString method
and s1 continues to point at "java".

However in case of array object or any other object, when you change object pointed by reference a1, it actually changing the actual object.
In java you pass object reference by value,

Thus at line:

public long[] changeLong(long[] a1) {...}

a1 is a new reference variable of type array of long pointing to the same long array object in the doLong object. and here whatever you change will change the original array object as it is NOT immutable.


I Hope this will help you..

K&B book nicely explained the java String object.


Arun (SCJP-1.4, IBM-141, SCWCD-1.4)
"How hard you try is rooted to how often you try" - Doug Hall"
K Patel
Greenhorn

Joined: Mar 29, 2004
Posts: 7
I think Arun gave an excellent explanation. Remember 2 points to this example whenever a string object is changed a new string object is created and remember you cannot change the refernce to a object even though an object can change values as in the array example. When you think about it, good thing you cant change what objs variables reference to, only the reference itself.
charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
Try Corey McGlone's Blog The SCJP Tipline where you have an excellent example which shows exactly what happens when strings are passed into methods as also arrays. The example there is much easier to vizualize
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing variables into methods
 
Similar Threads
Garbage collection
exam lab gc question
passing arrays
Array Question
Doubt over Garbage Collection