Java passed by reference or value transfer

concept

  1. Pointers and references

    First of java is not a pointer concept, but there is in c ++ pointers (*) and reference (&) concept, is not very good distinction, but to swap method, for example, then both can achieve the purpose of exchange. In order to better distinguish my first set

    Pointer: directly to the data, all changes are directly modify the data (but in fact is a pointer to the object, which the value stored address data)

    Reference: reference data is modified by a variable intermediate, indirectly through intermediate variables modified data values

  2. And transmitting values ​​passed by reference

    I think it's been passed by reference are passed by value and by swap function based on university courses, so I think

    Value is passed: no arguments and associated parameters, a method is analogous two variables, but the same value

    Passed by reference: arguments and associated parameters, the same address may be a method analogous two variables in the same object, but not the same reference name, but the reference value is

example

  1. Value transfer
public class Test {
    public static void main(String[] args) {
        int val = 20;
        Test.test(val);
        System.out.println("实参:" + val);
    }

    private static void test(int val) {
        val = 10;
        System.out.println("形参:" + val);
    }
}

For basic variables, arguments and parameters not associated, complementary effect of the output

Parameter: 10
arguments: 20

  1. Passed by reference
public class Test {
    public static void main(String[] args) {
        Stu stu = new Stu();
        stu.setAge(20);
        stu.setName("test");
        Test.test(stu);
        System.out.println("实参:" + stu);
    }

    private static void test(Stu stu) {
        stu.setName("test2");
        stu.setAge(10);
        System.out.println("形参:" + stu);
    }
}

class Stu {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Stu is an object, because of reference passed, the formal and actual parameters point to the same address, if the parameter by modifying the data, then the data pointed to by the argument may also change, since a point to the same address, so output

Parameter: Stu {name = 'test2' , age = 10}
argument: Stu {name = 'test2' , age = 10}

  1. Passed by reference
public class Test {
    public static void main(String[] args) {
        Stu stu = new Stu();
        stu.setAge(20);
        stu.setName("test");
        Test.test(stu);
        System.out.println("实参:" + stu);
    }

    private static void test(Stu stu) {
        stu = new Stu();
        stu.setName("test2");
        stu.setAge(10);
        System.out.println("形参:" + stu);
    }
}

class Stu {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Same code, but more than the upper row stu = new Stu();, but this time the modified parameter of the point, where the modified parameter, does not modify the data, after modifying the parameter points to a new address, so that the argument disconnect the association, so the result is

Parameter: Stu {name = 'test2' , age = 10}
argument: Stu {name = 'test' , age = 20}

  1. Passed by reference
public class Test {
    public static void main(String[] args) {
        String val = "20";
        Test.test(val);
        System.out.println("实参:" + val);
    }

    private static void test(String val) {
        val = "10";
        System.out.println("形参:" + val);
    }
}

For the String object, because it is a modified final, so regardless of any change in the operation of the new data is in a String pool, because there is no use new, so are pointing String constant pool, and a link to the "10 "a point of" 20 "

in conclusion

From the above examples 3 and 4 we see the process if the reference type of parameter in a " = " This operation, then the connection is disengaged arguments, parameters, any change does not occur, it is to be noted .

So my conclusion is that the basic types passed by value, type of object reference. I read a lot of value to say is passed, in fact, that also makes sense, but everyone has their own understanding, to solve production problems on the line.

Guess you like

Origin www.cnblogs.com/colin-xun/p/12656773.html