So its like a Copy constructor in C++ so to say???
Thanks for all the info guys!
--Shankar


-----Original Message-----
From: Kevin Mukhar [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 5:31 PM
To: JDJList
Subject: [jdjlist] Re: Java : pass by reference???


> Madhav Vodnala wrote:
> 
>     1)  Pass by reference works for all Classes, except Strings and arrays.

Wrong. Pass by reference works for all classes with no exceptions. The
difference is that 'pass by reference' means something different in Java than it
does in other languages. In Java, parameters passed to methods are method local
variables. If the variable passed to a method is an object reference, then the
method parameter is also a reference to the same object. Within the method, you
can call all the object's methods using the parameter. These methods will act
upon the object just as if you had called the methods from outside the method.
For example:

public void TestRef {
  public static void main(String[] args) {
    String myString = "Hello, World!";
    System.out.println(s); //prints Hello, World!
    System.out.println(s.charAt(0)); //prints H
    doSomething(MyString);
  }

  public void doSomething(String s) {
    System.out.println(s); //prints Hello, World!
    System.out.println(s.charAt(0)); //prints H
  }
}

Within the method doSomething(), we have a reference to the String "Hello,
World!". The variables s and myString both refer to the same String object. A
(copy of the) reference was passed to the method; that is what 'pass by
reference' means. Inside the method, we can call all the String methods that we
want and those methods will act upon the same String object.

However, since s is a COPY of the original reference, when you change the value
of s to point to a new object, it does NOT change the value of myString.

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