For anyone who's interested, here's a little example which shows how you can return a 
new value for a String via a method argument by using a String array as the argument 
type instead of String. 

The code:

  public static void main(String[] args)
  {
    class Test {
      void doTest(String[] a) {
        a[0] = "Value set in doTest()";
      }
    }

    String s = "main";
    String[] a = new String[] { s };
    System.out.println("s before doTest(): " + s);
    System.out.println("a[0] before doTest(): " + a[0]);

    Test t = new Test();
    t.doTest(a);

    System.out.println("s after doTest(): " + s);
    System.out.println("a[0] after doTest(): " + a[0]);
  }

And here's the output: 

        s before doTest(): main 
        a[0] before doTest(): main 
        s after doTest(): main 
        a[0] after doTest(): Value set in doTest() 

So you can get the new string value by just using the array to access it. 

Regards, 
Al.


-----Original Message-----
From: Madhav Vodnala [mailto:[EMAIL PROTECTED]]
Sent: 31 May 2002 13:22
To: JDJList
Subject: [jdjlist] Re: Java : pass by reference???



----- Original Message ----- 
From: H Shankaranarayanan 
To: JDJList 
Sent: Friday, May 31, 2002 4:11 PM
Subject: [jdjlist] Re: Java : pass by reference???


----Original Message-----
From: Madhav Vodnala [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 3:59 PM
To: JDJList
Subject: [jdjlist] Re: Java : pass by reference???


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.
[Shankar] Pass by reference works for all objects(incl arrays which are also objects
be it even primitive arrays.as by definition anything on the heap is an object and 
anything "new"ed
is on the heap)  And by inlining the code in the methods that am calling am 
able to change the value of the variables or objects to be precise. So why not from 
inside a method?
[Madhav]
Oops. My mistake. there is no exception for Strings or Arrays.
If you had run my code, its clear that inlining the code gives the same result as 
calling the
method. i.e, chaging the value of variables is not possible in both the cases.

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.
[Shankar] this is wot i infered from the behaviour of the code So then coming back to 
the "pass by reference"
for objects. This behaviour says its a "pass by value" and "pass by reference" correct?
[Madhav] 
Its always pass by reference if we accept all the objects(that are newed) are passed 
by reference.

Another interesting fact is a swap method (like the one u write in C to swap an 
integer value)
cannot be written in Java just for this one small behavioural issue of the language. I 
dont know 
if its an issue for starters. u cant just swap two strings inside a called method cos 
of this.
[Madhav]
Yeah. right. I see this because Java doesnt have pointers. In C you can have local 
variables of a function
refer to calling method's variable value, where in java, you can only have the local 
variables refer the calling method's variable (which is a reference itself).

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 

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to