Review: Java method parameter passing mechanism

A process parameter passing

  Parameter for the basic data types: data value

  Parameter reference data types: address values, String, etc. immutable objects packaging

 

Second, the actual case

public class TestArgs {

    public static void main(String[] args) {
        int i = 1;
        String str = "hello";
        Integer j = 10;
        Integer[] array = {1, 2, 3, 4 ,5};//引用类型存在于堆上
        MyData myData = new MyData();

        foo(i,str,j,array,myData);

        System.out.println("i="+i);
        System.out.println("str="+str);
        System.out.println("j="+j);
        System.out.println("Array =" + of Arrays.toString (Array)); 
        System.out.println ( "mydata.i =" + myData.i); 
    } 

    public  static  void foo ( int I, S String, J Integer, Integer [] Array , the myData myData) { 
        I + =. 1 ; 
        S + = "World" ; 
        J + =. 1 ; 
        Array [ 0] = +. 1 ; 
        myData.i + =. 1 ; 
    } 

} 

// the myData object present in the heap, its properties are also present in the heap 
class the MyData {
     int I =. 5 ; 
}

 

Operating results as follows:

i = 1
str = hello
J = 10
array = [2, 2, 3, 4, 5]
mydata.i = 6

Guess you like

Origin www.cnblogs.com/noperx/p/11316692.html