Page 1 of 1

Re: About Question com.enthuware.ets.scjp.v6.2.694 :

Posted: Wed Sep 26, 2012 7:21 am
by Guest
I tried the code snippet.
Out printed is "Hello world" and not "Good bye world"
String is passed by value and not as a value of the reference.
Did i forgot something ?

Code: Select all

public class TestReference {
    static String str = "Hello World";

    public static void changeIt(String s) {
        s = "Good bye world";
    }

    public static void main(String[] args) {
        changeIt(str);
        System.out.println(str);
    }
}

Re: About Question com.enthuware.ets.scjp.v6.2.694 :

Posted: Wed Sep 26, 2012 9:13 am
by admin
String is an Object, so it is not passed by value. Its reference is passed by value.

HTH,
Paul.