Why only Java value passed?

1. The value passed to the concept of reference passed

Before passing way to understand what shaped involved in the argument.

In the form of parameters : the parameter is used in the definition of the function names and function of the body when the object is used to receive incoming calls when the function parameters.

Actual parameters: calling the function with a time reference, the data transfer relationship between the calling function and the called function. When you call a function in the calling function, the function name in brackets after the parameter called "actual parameters."

It can be appreciated that: the parameter is a abstract argument, the argument is a call parameter, the parameter is a parameter defined function

Value is passed: method call, passing it the actual parameter values to the corresponding formal parameters, is a function of the received copy of the original value, in which case there are two identical memory basic types , i.e., formal parameters and actual parameters, back the method of operation is to modify the parameter value, it does not affect the value of the actual parameter.

Passed by reference: refers to when calling the function directly address the actual arguments passed to the function, then to modify the parameters carried out, it will affect the actual parameters in functions.

: Draw the focus of the main difference between the transfer value and passed by reference

Value transfer

Passed by reference

Create a copy, you can not change the original value in the function body

Without creating a copy, the original value can not be changed in the function body

 Create a copy of meaning Figure:

Create a copy is to say, the arguments to call out to copy, then copy that a pass into the body of the function. When not creating a copy, copy this step does not occur.

For value passed chestnut:

public class Test01 {
    public static void main(String[] args) {
        int a=1,b=2;
        swap(a, b);
        System.out.println("a="+a);
        System.out.println("b="+b);
    }
    public static void swap(int a,int b) {
        int temp=a;
        a=b;
        b=temp;
    }
}

result:

a=1
b=2

Chestnut above, so that the function implemented in a, b exchange, but after the function call, the result is still output a, b of the original value, the operation of the function does not affect the body in a, b values ​​in vitro function.

Chestnuts not pass a reference test, there may be interested to test With c ++, defined as an alias for the parameter or pointer, in c ++ is passed by reference.

Myth: parameters passed if it is a common type that is passed by value, and if the object that is passed by reference. This is wrong! ! ! ! !

The value passed 2.java

 In java, regardless of the type of parameters are the basic or reference data types, are passed by value. For reference to the following types of data parameters, we have a basic data type parameter passing chestnut above.

public class Test01 {
    public static void  swap(Student st1,Student st2) {
        Student temp=st1;
        st1=st2;
        st2=temp;
    }
       
    public static void main(String[] args) {
        Student st1=new Student("张三",20);
        Student st2=new Student("李四",20);
        swap(st1, st2);
        System.out.println("st1:"+st1);
        System.out.println("st2:"+st2);
}

result:

st1: Student [name = Joe Smith, Age = 20 is ]
 ST2: Student [name = John Doe, age = 20]

Example, the object pointed st1 and st2 is not changed.

At this time, you might ask, since java is passed by value, so real participants copies happen, that copy is what is it? The answer is: a copy of the address of the object is on the heap. To a chestnut to verify:

{class to Test01 public
    public static void Print (Student STU1, Student STU2) {
        Student TEMP = STU1;
        STU1 = STU2;
        STU2 = TEMP;
        System.out.println ( "Print STU1 after exchange function body:" + stu1);
        System.out.println ( "after exchange of the function body printing STU2:" + STU2);
    }
       
    public static void main (String [] args) {
        Student STU1 new new Student = ( "STU1", 20 is);
        Student = new new STU2 Student ( "STU2", 30);
        Print (STU1, STU2);
    }
}

result:

After the exchange of the printing function body STU1: Student [name = STU2, Age = 30 ]
 after printing exchange function body stu2: Student [name = stu1, age = 20]

As can be seen from the results, in the body of the function stu1 and stu2 pointed object does is changed, because the copy of their address in the stack during the transfer of value.

Take a look at them in the memory is how:

At this time you might ask, since the value passed java is a copy of an address, then I can not change the content of the address pointed to? The answer is: Of course

static void changeInf public (Student STU) {
        stu.setName ( "I changed the name");
    }
       
    public static void main (String [] args) {
        Student Student new new STU = ( "Joe Smith", 18 is);
        changeInf (STU );
        System.out.println (STU);
    }

result:

Student [name = I change the name, age = 18]

Object content changed

Conclusion: java only value is passed, it may be because there is no reason java alias pointers and references bar.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160192.htm