Hi Shankar
Here is my take on 'pass by reference'
theory.
Here are the absolute statements about 'pass by
reference'
1) Pass by
reference works for all Classes, except Strings and arrays.
2 ) Primitive data types are
always passed by 'value'.
I think this is very straight forward and can be
verified with any small example.
Now lets analyze your code.
Check out the following code. I just replaced the
method call with its equivalent
code. I mean the code is exactly the same as
'calling' the method.
public static void main (String[]
args)
{
test objTest = new
test();
String testfill = new String("main");
// objTest.fill(testfill);
String testfill = new String("main");
// objTest.fill(testfill);
String
objTest_fill_fillMe = testfill;
objTest_fill_fillMe =
"test"; // same as objTest_fill_fillMe = new String
("text");
System.out.println("Fill me result:" + testfill);
Integer intFill = new Integer(200);
// objTest.fill(intFill);
Integer
objText_fill_fillMe = intFill;
objTest_fill_fillMe =
new Integer (100);
System.out.println("Fill me result:" + intFill);
}
Now, in first case, String variable testfill is not
passed by reference.
secondly, local variable 'fillMe' first refers to
the value that is being passed and
refers the new object created with
new. so its nothing but changing the local variable
value.
SO, the theory is
If you assign the new object (created
with new ) to the function argument (which refers to the
calling method variable) and modify its
content, it doesnt modify the calling method's
variable value, irrespective of whether calling method's variable is passed
by value or passed by
reference.
-Madhav
----- Original Message -----
From: "H Shankaranarayanan" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Friday, May 31, 2002 1:00 PM
Subject: [jdjlist] Java : pass by
reference???
> {
> /**
> * Description of the Method
> *
> *@param fillMe Description of the Parameter
> */
> public void fill(String fillMe)
> {
> fillMe = "test";
> }
>
>
> /**
> * Description of the Method
> *
> *@param fillMe Description of the Parameter
> */
> public void fill(Integer fillMe)
> {
> fillMe = new Integer(100);
> }
>
>
> /**
> * Description of the Method
> *
> *@param args Description of the Parameter
> */
> public static void main(String args[])
> {
>
> try
> {
> test objTest = new test();
> String testfill = new String("main");
> objTest.fill(testfill);
> System.out.println("Fill me result:" + testfill);
>
> Integer intFill = new Integer(200);
> objTest.fill(intFill);
> System.out.println("Fill me result:" + intFill);
>
> } catch (Exception e)
> {
> e.printStackTrace();
> }
> }
> }
>
>
> That is a sample program i wrote to test this fact. The result is dependent
> on scope of the variable.
> So wots this pass by reference concept that every text book around the world
> states about Java.
>
> How does the pass by reference concept work anyways?
>
> I might have missed something here. If i did i would appreciate if anyone
> told me wot is it that i did miss.
>
> I was expecting this program to work otherwise but it does not.
>
> --Shankar
>
>
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
