Please indicate the results of the following procedures? (a)

public class Example {
    String str = new String("good");
    char[] ch = {'a'.'b'.'c'};
	
    public static void main(String[] args) {
        Example example = new Example();
        example.change(example.str, example.ch);
        System.out.print(example.str + " and ");
        System.out.print(example.ch);
    }

    public void change(String str, char[] ch) {
        str = "test ok";
        ch[0] = 'g'; }}Copy the code

A. good and abc

B. good and gbc

C. test ok and abc

D. test ok and gbc

Reference data type value transfer and immutable string object.

First, let’s look at the main method. In the main method, we create an object of the current class Example, call its change method, pass in its two member variables that reference data types, and print the values of the two member variables after calling change.

As we all know, in Java references to datatype values are passed memory addresses, so in the change method, the two parameters (local variables, whether or not they have the same name as member variables, variables declared in the method are local variables, Included in the method body declaration as well as the method parameters) each point to the same memory space as the two member variables point to.

Therefore, after the local variable ch in the change method changes the data at index 0, the member variable CH is also affected because they point to the same memory space.

We also know that before JDK 1.9, the String type (String) was internally stored as an immutable array of characters.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
    / /...
}
Copy the code

In JDK 1.9, after investigation and analysis, JDK developers changed the content storage mode of String type (String) to immutable byte array in order to reduce the memory space waste caused by the original character storage.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    @Stable
    private final byte[] value;

    private final byte coder;

    @Native static final byte LATIN1 = 0;
    @Native static final byte UTF16  = 1;

    static final boolean COMPACT_STRINGS;

    static {
        COMPACT_STRINGS = true;
    }
    / /...
}
Copy the code

But anyway, the point is that strings are stored inside immutable arrays, which means that string objects are immutable, and if the contents change, a new string object is created.

Ok, let’s go back to this problem. The change method changes the string contents of the local variable STR, and since it involves changing the string contents, it creates a new string object and assigns its memory address to the local variable STR, The local variable STR has no relation to the member variable STR, and the content change action does not affect the member variable STR.

So, when the change method is called in the main method, the result of printing the values of the member variables STR and ch is good and GBC.