java parameter, argument, passed by value, passed by reference

Work, found that most people passed by value, not deep understanding passed by reference, the following articles to write again to talk in detail.

One, Katachisan

Parameter: the parameters passed to the call receiving method is called only when the allocated memory, once the call ends, releases the memory space. Thus the method is valid only.

public void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

这里a,b就是形参,方法结束时,在栈空间就会被销毁。

Second, the argument

Calling the method swap (6, 8) above; wherein 68 is the argument.

Third, the value of the transmission and passed by reference

  • Value is passed: method call, the actual parameter values ​​to the form of its argument, the function receives a copy of the original value, in which case the same two basic types in memory, when the process performs processing operations on the parameter and will not affect the value of the actual parameter.
  • Passed by reference: method call, refer to an actual process parameters (values ​​refer to the memory address, not the parameters) are transmitted to the corresponding formal parameters, is a function of received memory address of the original value, in the method, the parameter and (address) of the contents of the same parameters, the method affect the value of the parameter argument will process.

public class People {

    private String name;
    private String age;

    public People(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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


public class Base1 {
    
    public static void main(String[] args) {

        int a = 10;
        // 按值传递
        test1(a);
        System.out.println("main a==" + a);
        People p = new People("rose", "18");
        // 按引用传递
        test2(p);
        System.out.println("main name==" + p.getName() +", age==" + p.getAge());
    
    }

    public static void test1(int a) {
        a = 20;
        System.out.println("test1 a==" + a);
    }

    public static void test2(People p) {
        p.setName("jack");
        p.setAge("19");
        System.out.println("test2 name==" + p.getName() +", age==" + p.getAge());
    }
}

输出结果:
test1 a==20
main a==10
test2 name==jack, age==19
main name==jack, age==19

in conclusion:

  • When the parameter is the primitive type parameter of the process will not affect the argument.
  • When the parameter for the type of application, the influence of the parameter arguments will process.
  • String, Integer, Double like immutable types, because the operation itself does not provide the modification function, each operation is to generate a new object, so to special treatment, as will be appreciated the value passed, does not affect the operation parameter argument object.

Four, java objects and references

People with the above example, build an object: People p1 = new People ( " jack", "19");
In fact, this object contains four movements.

  • The right of the new People, People class is a template, create a class object in the People heap space.
  • At the end ( "jack", "19"), refers to the object is created, the constructor call, just generated object is initialized.
  • People p1 the left created a People class reference variables, the so-called People class reference, is that in future can be used to point to People object.
  • = Operator so that the object pointed object reference People just created.
People p1;
p1 = new People("jack", "19");
上面两条语句等价于 People p1 = new People("jack", "19");

这样看就很明白,一个是对象应用变量,一个是对象实体。
如果只执行第一条,还没执行第二条此时创建的p1还没指向任何一个对象,它的值是null。

再来一句:
People p2;
p2 = p1;
这里发生了复制行为,对象本身没有复制,被复制的只是对象的引用。结果是p2也指向了p1的对象。

再来一句:
p2 = new People("rose", "18");
此时p2指向了第二个对象,此时得出结论:
一个对象引用可以指向0个或者1个对象。
一个对象可以有N个引用指向它。

再来一句:
p1 = p2;
此时p1指向p2这个对象,至于之前的p1这个对象,它已成为垃圾回收机制的处理对象。

Four, java method parameters passed by reference

"When an object is passed as a parameter to a method", which is called the reference.

public class Base2 {
    public static void main(String[] args) {
        People p = new People("jordon","23");
//        changePeople1(p);
        changePeople2(p);
        System.out.println(p.getName() + "===" + p.getAge());
    }

    public static void changePeople1(People p) {
        p.setName("kobe");
        p.setAge("24");
    }

    public static void changePeople2(People p) {
        p.setName("kobe");
        p.setAge("24");

        p = new People("james", "24");
    }
}

以上代码调用changePeople1和changePeople2都是输出
kobe===24
我们来分析下为什么了??

Guess you like

Origin www.cnblogs.com/wudiffs/p/11573314.html