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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Guest

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

Post 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);
    }
}

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

String is an Object, so it is not passed by value. Its reference is passed by value.

HTH,
Paul.

nnavlani
Posts: 3
Joined: Sat Aug 02, 2025 11:21 pm
Contact:

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

Post 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).

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Why do you think it is incorrect? What happened when you tried to compile and run the given code?

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests