Personal understanding of Java, by value

Foreword

Value transfer

Value transfer means when the actual parameters of the function call to copy a parameter passed to, so that the function of the parameter changes will not affect the value of the actual parameter.

Passed by reference

Reference transfer means when calling the function address of the actual argument passed directly to the parameter, the parameters for the modification will affect the value of the actual parameter in the function.

We can use a program to verify Javaonly the value transfer

/**
 * 验证java中只有值传递
 * Dmego 2018-8-27
 */

class User{
    
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class TestValue {
    public static void change(User user2,int a2){
        System.out.println("改变之前:"+user2.getName()+",a2="+a2);
        
        user2.setName("李四"); //改变 user2 的 name 值
        a2 = 10; //改变 a2 的值
        System.out.println("改变之后:"+user2.getName()+",a2="+a2);
        
        user2 = new User(); //将 user2 重新指向一个新对象
        user2.setName("王五");
        System.out.println("重新指向一个新对象后:"+user2.getName());
    }

    public static void main(String[] args){
        User user1 = new User();
        user1.setName("张三"); //初始化 user1 的 name 为张三
        int a1 = 5; //初始化 a1 的值为 5
        change(user1,a1); //调用方法验证传值方式
        System.out.println("调用方法后:"+user1.getName()+",a1="+a1);
    }
}

Run this program, the output is:

改变之前:张三,a2=5
改变之后:李四,a2=10
重新指向一个新对象后:王五
调用方法后:李四,a1=5

Result analysis

Heap and Memory Analysis

Let auxiliary graph above, to analyze this procedure, we first define a Userclass, then the test in a class is instantiated Userobject name user1, and assign it name = '张三', this time in memory as 图1shown instantiated an object is equivalent to the heap opened up a memory, the memory address 017, this time the object is referenced user1memory address 001, which holds the address of the object in memory, which is the point of the object. Took over, we call the method change()to try to change user1the namevalue of this validation javaby value in.

We will user1as an argument passed to change()a method, parameters user2to accept the arguments, here embodied in the two different ways of transmission parameters. If it is passed by value, so as defined above, as 图2shown, user2is user1a copy, that is to say at the time of passing parameters, user1(itself a reference to an object), a copy called user2it It is also a reference to the object, and user1and user2at this time point to the same object. And if it is passed by reference, also as defined above, as 图5shown, when the transmission parameters, is directly user1transmitted to the parameter, only a name is changed user2, but essentially user1and user2in fact the same. It is a reference to an object.

Next to analysis of the output, whether it is passed by value or by reference, the results of the first output line is constant 张三, since both refer to the same object. For the second line of output, we still can not determine which way, because the same object is changed, the value will change; the key is to line 3 and 4 output lines output at this time, we will be user2redirected to a new objects, and assigned to the object name = '王五', if it is 引用传递the way it user1will also change to point, point to the new object, the result output after the last row will call the method and the third line is the same 王五, but the fact is output 李四, this suggests user1and user2is actually not the same. True calling process as 图2~ 图4shown, this will make user2the point to a new object, user1the object pointed to does not change, or the original object.

For basic types of parameters, the a1value does not change the last described method when executed, a2it is a1a copy. For reference types of parameters for such Useran object, a method call, in fact, is its reference user1as an actual argument, then passed to the parameter will be a copy of the reference cited user2, although it is two references ( like a1the a2relationship). But point to the same object, all of these operations are also in terms of the same object.

Finally, an example to illustrate the image of all of this, if you have a key to your room and in your name engraved on it, the process is like for a inttype of a1initialization value 5. Your friends and your relationship is very good, you want your room key, then you do not put your keys directly to him, but a copy of the new key, this key is also able to open the door of your room. And your friends in this the new key engraved with his name. This process is like calling change()method to a1copy an assignment to a2, then modify a2and a1nothing to do, your friend engraved his name on the new keys in your hand that will not affect the original key. The key is that the keys can open your room, like user1and user2point to the same object. At this point your friend used to get a new key to open your room, your room TV smashed. This process is like renamed 李四. Then you take your room key to open you will definitely see such a scene - the TV smashed. Like after calling the method user1becomes 李四. During the method call, the last user2redirected to a new object, which is like your friend will you give him a copy of the keys have been processed again, this time not to open the door of your room, but able to open his own room he opened his room with this key and then smashed his own TV which does not affect the TV in your room, that is to say the last user1name and does not become 王五. This is javathe value of the transfer. Of course, if it is passed by reference, then the case from beginning to end will be only one key, the final results will be different.

end

We can know by the above analysis. JavaOnly values are passed this kind of way, but for reference types, the parameter passing is a reference to an object of Bale.

Guess you like

Origin www.linuxidc.com/Linux/2019-12/161792.htm