• Java中参数的传递方法


    Java语言中参数传递是常见的问题之一,通过阅读如下测试代码,可以看看你是否真的了解java常见的参数传递的规则。

    Java代码:

    public class TestChangeParamInt {
    
        public static void main(String[] args) {
            int n = 3;
            System.out.println("Before change : " + n);
            
            changeData(n);
            System.out.println("After changeData(n) : " + n);
        }
    
        public static void changeData(int n) {
            n = 10;
        }
    
    }
    
    public class TestChangeParamObj1 {    
    
        public static void changeData(StringBuffer strBuffer) {
            strBuffer.append("World!");
        }
    
        public static void main(String[] args) {
            StringBuffer strBuffer = new StringBuffer("Hello ");
            System.out.println("Before change : " + strBuffer);
    
            changeData(strBuffer);
            System.out.println("After changeData(n) : " + strBuffer);
        }
    }
    
    
    public class TestChangeParamObj2 {
    
        public static void main(String[] args) {
            StringBuffer strBuffer = new StringBuffer("main ");
            System.out.println("Before change = " + strBuffer);
            
            changeData(strBuffer);
            System.out.println("After changeData(n) = " + strBuffer);
        }
    
        public static void changeData(StringBuffer strBuffer) {
            strBuffer = new StringBuffer("Change ");
            strBuffer.append("World!");
        }
    
    }
    
    public class TestChangeParamObj3 {
    
        public static void main(String[] args) {
            StringBuffer strBuffer = new StringBuffer("Hello ");
            System.out.println("Before change = " + strBuffer);
    
            changeData(strBuffer);
            System.out.println("After changeData(n) = " + strBuffer);
        }
        
        public static void changeData(StringBuffer strBuffer) {
            StringBuffer sb2 = new StringBuffer("change ");
            strBuffer = sb2;
            sb2.append("World!");
            System.out.println("In changeData(n) = " + sb2);
        }
    
    }
  • 相关阅读:
    误删本地分支,两天的工作量差点毁于一旦,使用git reflog 有惊无险恢复
    单点登录SSO
    IdentityServer4介绍和使用
    常见的远程无线通信技术
    4G模块和DTU模块有何区别
    以太网IO控制器
    什么是无线网关,无线网关的功能
    什么是IOT网关,网关功能有哪些
    Modbus RTU和Modbus TCP的两种区别
    数据采集网关设备 如何选择数据采集网关
  • 原文地址:https://www.cnblogs.com/90zyh/p/3046842.html
Copyright © 2020-2023  润新知