This is the second day of my participation in Gwen Challenge

I found a problem when I did PTA’s Java homework yesterday, as shown in the picture below:

At the beginning of this one, I thought it was choice C, and the output was “AB.AB”. I’m thinking of String as a reference data type, which should be passed by reference as an argument to a method. So when you operate on String x and y, you change the real value.

However, the actual results are not what I thought……

Type String

First, String stands for String. Is a final class that represents an immutable sequence of characters. Strings are constants and are represented by double quotes. Values cannot be changed after creation. The String contents of a String object are stored in a character array value[].

Instantiation of String

The way literals are defined

String STR = “hello”;

First, the data defined by the literal is in the string constant pool in the method area, meaning that the same string shares a single address value. To explore how String is passed as a parameter in a method, first try to pass literal Sring type variables as parameters

public class Main{
    public static void main(String args[ ]){
        String a ="A";
        String b = "B";
        mb_operate(a,b);
        System.out.println(a + "." + b);
    }
    static void mb_operate(String x,String y){ x=x.concat(y); y=x; }}Copy the code

As you can see from the answer, the String type defined by a literal is passed as a value as a method parameter.

Through the constructor

String s1=new String();

New constructor declaration data, stored in a heap, this statement when two of the same String variable, in the heap space will open up the two Spaces, the address is not the same, this is the case in the topic, however, through practice, you can know the constructor definition of type String as method parameters is also in the form of value transfer.

public class Main{
    public static void main(String args[ ]){
        String a = new String("A");
        String b = new String("B");
        mb_operate(a,b);
        System.out.println(a + "." + b);
    }
    static void mb_operate(String x,String y){ x=x.concat(y); y=x; }}Copy the code

To sum up, String is a reference data type, but its variables are passed in value as arguments to methods, just like basic data types.

String [] is a wrapper class that uses a char[] as the base layer of a String. String [] is a wrapper class that uses a char[] as the base layer of a String. Here’s what he said:

String is equivalent to a wrapper class for char[]. One of the qualities of a wrapper class is that it behaves like its corresponding primitive type when operating on its value. This is what the wrapper class does when passing parameters. So, an explanation of what String would represent in this case comes naturally. Similarly, Integer, Float, and other wrapper classes behave the same as String in this case.

Attached is the portal to the Big Guy blog

Is there a way to make a String variable pass by reference as an argument?

Method 1: Use the StringBuffer variable string type

public class Main{
    public static void main(String args[ ]){
        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");
        mb_operate(a,b);
        System.out.println(a + "." + b);
    }
    static void mb_operate(StringBuffer x,StringBuffer y){ x=x.append(y); y=x; System.out.println(y); }}Copy the code

Note that the increment in StringBuffer is append, and when y=x, the output is ab.b, and the assignment to the data does not affect the actual value. Using the method in StringBuffer on method y, you can see that the value of b is also affected

public class Main{
    public static void main(String args[ ]){
        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");
        mb_operate(a,b);
        System.out.println(a + "." + b);
    }
    static void mb_operate(StringBuffer x,StringBuffer y){
        x.append(y);
        y.insert(1."BBB"); }}Copy the code

I guess it’s because methods in StringBuffer operate directly on references, whereas normal assignments don’t affect value changes or address changes…

// Updated the blog posted in the morning on June 2, 2021. In the afternoon, when I continued to do the questions, I found this type of questions again:……

There are two ways to implement a String variable as a pass-by-reference as an argument: You can either use StringBuffer directly, or you can store a String into an array of strings and use the array as an argument to achieve the effect of passing a reference.

Method two: Use the String[] array

public class Main {
    public static void main(String[] args) {
        String a = new String("A");
        String b = new String("B");
        String[] s=new String[2];
        s[0] = a;
        s[1] = b;
        mb_operate(s);
        System.out.println(s[0] + "." + s[1]);
    }
    static void mb_operate(String[]s){
        s[0]=s[0].concat(s[1]);
        s[1]=s[0]; }}Copy the code

As for more parameter transfer problems of String, I will continue to explore them in my future study, and will continue to improve the mistakes of this article and supplement the shortcomings of this article.