java with reference to the basic data transfer type transfer difference

Disclaimer: This article is the original article CSDN bloggers "zejian_", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/javazejian/article/details/51192130

 

java and deliver value passed by reference in the interview are usually involved, today we have to talk about this issue, we must first recognize that this problem is usually a function of the relative terms, that is in the java method parameters, then let's take a look at two technical terms about the parameters passed to the method (or function) in programming languages:

Call by value (call by value)

Call by reference (call by reference)

The so-called call by value representation received is the value provided by the caller, and the call by reference, said method receives a variable address supplied by the caller (if the C language's words pointer matter, of course, java and no pointers concept). Here we need to note that a method can modify the values ​​of variables corresponding to pass a reference, but can not modify the values ​​of variables to pass values ​​corresponding to the call, this sentence is very important, this is the fundamental difference between the call by value and call by reference, of course, if I do not understand, it does not matter, we must thoroughly analyze the following illustrations of friends.

We said earlier java does not exist in reference to the calling, this is wrong, because the java programming language is indeed using call by value, namely call by value. That method is to obtain a copy of all the parameter values, the method does not modify any of it is passed to the parameter variables. Let's look at an example:

package com.zejian.test;
/**
 * Java in the call by value
 * @author zejian
 */
public class CallByValue {
    
    private static int x=10;
    
    public static void updateValue(int value){
        value = 3 * value;
    }
    
    public static void main(String[] args) {
        System.out.println ( "call before the value of x:" + x);
        updateValue(x);
        System.out.println ( "value of x after the call:" + x);
    }
    
}

Run the program, results are as follows:

Called before the value of x: 10

The value of x after call: 10

You can see the value of x does not change, then we look together specific implementation process:

 

analysis:

1) value is initialized to a copy of the value x (i.e. 10)

2) value is multiplied by 30 equals 3, but note that the value of x at this time is still 10!

After 3) of this method, the variable parameter value is no longer used, were recovered.

Conclusion: When the basic type delivery method parameter data type (numeric, and boolean value), a method is possible to modify a basic data type parameter.

Of course there are reference data types java in addition to basic data types, that is, an object reference, then for this type of data is kind of how the situation? As we look at an example:

Declare a User object type:

package com.zejian.test;
public class User {
    private String name;
    private int age;
    public User(String name, int age) {
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Execution classes are as follows:

package com.zejian.test;
/**
 * Java in the call by value
 * @author zejian
 */
public class CallByValue {
    private static User user=null;
    public static void updateUser(User student){
        student.setName("Lishen");
        student.setAge(18);
    }
    
    
    public static void main(String[] args) {
        user = new User("zhangsan",26);
        System.out.println ( "call before the user's value:" + user.toString ());
        updateUser(user);
        System.out.println ( "user value after the call:" + user.toString ());
    }
}

Results are as follows:

Value before calling user: User [name = zhangsan, age = 26]

The value after calling user: User [name = Lishen, age = 18]

Obviously, the value of the User's been changed, that is to say if the method parameter type is a reference type, then type the corresponding reference value will be changed, let's analyze this process:

 

Process analysis:

1) student user variable is initialized to a value of the copy, this is a reference to the object.

2) call set methods student variables acting on the reference object, User objects and student user while the internal reference value is modified.

After 3) methods, student variable is no longer used, it was released, and the user is not changed, still pointing to the User object.

Conclusion: When the delivery method is a reference type argument data type, a method for modifying a reference parameter value of the data type of the object pointed.

While here two types of data are transmitted over the analysis, the basic data types also understand the difference between data transfer and pass a reference type, the former will not modify the value of the original data, which will modify the reference value of the object pointed . By the above examples we might feel that java also has call by value and call by reference ah, a pity that such understanding is misleading, reflecting the call by reference cited above, although the phenomenon passed on the surface, but in java It does not only call by value and call by reference. Here are the estimated number of people forced to Mongolia, here we passed a counter-example to illustrate (recall the beginning of the call by value and call by reference fundamental difference we explained).

package com.zejian.test;
/**
 * Java in the call by value
 * @author zejian
 */
public class CallByValue {
    private static User user=null;
    private static User stu=null;
    
    /**
     * Two objects exchange
     * @Param x
     * @param y
     */
    public static void swap(User x,User y){
        User temp =x;
        x = y;
        y=temp;
    }
    
    
    public static void main(String[] args) {
        user = new User("user",26);
        stu = new User("stu",18);
        System.out.println ( "call before the user's value:" + user.toString ());
        System.out.println ( "call before stu value:" + stu.toString ());
        swap(user,stu);
        System.out.println ( "user value after the call:" + user.toString ());
        System.out.println ( "stu value after the call:" + stu.toString ());
    }
}

Let's swap two variables user and stu value through a swap function in front of us said that if it is to call a method can be modified by reference pass a reference variable corresponding to the value, which means that if java is a call by reference words then swap method will enable the exchange of data, and actual operating results are:


Value before calling user: User [name = user, age = 26]

Before the call stu value: User [name = stu, age = 18]

After calling the value for user: User [name = user, age = 26]

After calling stu value: User [name = stu, age = 18]

We found that the value of user and stu has not changed, that is, the method does not change the object stored in the variable user and stu references. X swap method parameters and y are initialized to two copies of the referenced object, this method is the value of this exchange of two copies of it, ultimately, what did was in vain Bale. At the end of the method x, y will be discarded, and the original variables user and stu still referenced objects referenced before this method is called.

 

This process also underlined the java programming language used is not a reference to the object invocation is actually an object reference passed by value is carried out, of course, where we can understand that this is the difference between simple call by value and call by reference, and must understand java function even in passing reference data types, just copy the reference value of nothing, we have been able to modify the reference data because they also point to an object, but this is still not a reference call by value call.

to sum up:

A method can not modify a basic data types of the parameters (numeric, and boolean).

A way to modify the state of the object a reference points, but still call by value instead of reference call.

The above two kinds of transfer processes have been copied value.

 

References: java core Volume 1

 

Guess you like

Origin www.cnblogs.com/doyi111/p/11703586.html