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: 10388
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests