Java难点,函数参数传递引用传递,值传递的理解

Java难点,函数参数传递引用传递,值传递的理解

案例一:输出结果为

add: 11
main: 10

package com.day1204;

public class MethodTest01 {
    public static void main(String[] args) {
        int x = 10;
        add(x);
        System.out.println("main:	"+x);
    }

    public static void add(int x) {
        x++;
        System.out.println("add:	" + x);
    }
}

案例二:输出结果为

add: 11
main: 11

package com.day1205;
public class MethodTest02 {
    public static void main(String[] args) {
        Person p = new Person();
        p.age = 10;
        add(p);
        System.out.println("main:	"+p.age);
    }
    public static void add(Person p) {
        p.age++;
        System.out.println("add:	"+p.age);
    }
}
class Person {
    int age;
}

自己去想为什么,别人告诉你的没用。

原文地址:https://www.cnblogs.com/yigongzi/p/14090408.html