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.

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

Posted: Thu Sep 04, 2025 12:10 pm
by nnavlani
So is the answer we get from Enthuware Incorrect? (Also, that both the method changeIt and variable str are static, and so any change there remains on the original variable).

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

Posted: Thu Sep 04, 2025 9:22 pm
by admin
Why do you think it is incorrect? What happened when you tried to compile and run the given code?