JAVA value transfer, passed by reference

Just writing a use case, we need to change the value of the parameter passed in the method, but only for a call-java, no pass-call. Therefore, changing the values of the parameters in the java method is not feasible. But you can change the property values of reference variables.
Be careful to understand what the following words:

1、对于基本类型参数,在方法体内对参数进行重新赋值,并不会改变原有变量的值。
2、对于引用类型参数,在方法体内对参数进行重新赋予引用,并不会改变原有变量所持有的引用。 
3、方法体内对参数进行运算,不影响原有变量的值。 
4、方法体内对参数所指向对象的属性进行操作,将改变原有变量所指向对象的属性值。 

That is, the basic data types, traditional values is achieved, only a parameter does not change the original value. A reference to the data type of this reference operates, actually corresponds to the operation of the parameter will not change the original reference. However, when the property referred to this operation, the equivalent of pass-CPP in the call, you can change the value of the referenced property.
for example:

public class Main {  
  
    private static void getMiddleOne(boolean b, Boolean boo, Boolean[] arr){  
        b = true;  //形参,不会改变原有值  
        boo = new Boolean(true);  //引用变量的直接操作相当于值传递,不会改变原来的引用变量  
        arr[0] = true;  //引用变量的属性的操作,会改变原有引用的属性,相当于传址调用  
    }  
      
    //测试  
    public static void main(String[] args) {  
        boolean b = false;  
        Boolean boo = new Boolean(false);  
        Boolean[] arr = new Boolean[]{false};  
  
        getMiddleOne(b, boo, arr);  
          
        System.out.println(b);    
        System.out.println(boo.toString());  
        System.out.println(arr[0]);  
  
        /** 
         * output: 
         *      false 
         *      false 
         *      true 
         */  
    }  
}  

Guess you like

Origin www.cnblogs.com/eternityz/p/12239576.html