JAVA的参数传递方式

(1)简单数据类型作为参数传递  “复制传值方式”,但是String类型很特殊,跟简单数据类型一样

package cn.edu.lei;

public class Test2 {

     void fun(int j)
        {
            j=20;
            System.out.println("fun函数后参数a的值:"+j);
        }    
        
        public static void main(String[] args) {
            int a=10;
            System.out.println("初始阶段a的值:"+a);
            Test2 test=new Test2();
            test.fun(a);
            System.out.println("调用函数后a的值:"+a);
            
        }

}

执行结果:

初始阶段a的值:10
fun函数后参数a的值:20
调用函数后a的值:10

(2)复杂数据类型作为参数传递 “引用传递方式”

package cn.edu.lei;

public class Test {

     void fun(Mytype j)
    {
        j.a=2;
        System.out.println("fun函数后复杂数据类型参数j的值:"+j.a);
    }    
    
    public static void main(String[] args) {
        Mytype type=new Mytype();
        System.out.println("初始阶段复杂数据类型type的值:"+type.a);
        Test test=new Test();
        test.fun(type);
        System.out.println("调用函数后type的值:"+type.a);
        
    }
}

class Mytype{
    public int a;
    public Mytype(){
        a=1;
    }
}

执行结果:

初始阶段复杂数据类型type的值:1
fun函数后复杂数据类型参数j的值:2
调用函数后type的值:2

原文地址:https://www.cnblogs.com/Yogurshine/p/2840988.html