A, the conclusion

In Java, the value is passed directly if the object is of an underlying type (deep copy), and the address of the object is passed if new is required (shallow copy). This also bothered me for a long time, c language has Pointers to distinguish, and Java does not have Pointers, so it is hard to distinguish.

Second, the analysis

1. Go straight to the code

package test;

/ * * *@authorXXJ * Verifies the Java value transfer method */
public class TransmitValue {
    private int a=0;
    private MyClass m=new MyClass();
    public static void main(String[] args){
        TransmitValue tv=new TransmitValue();
        tv.changeInt(tv.a);
        tv.changeMyClass(tv.m);
        System.out.println("tv.a:"+tv.a);
        System.out.println("--");
        System.out.println("tv.m:"+tv.m.i);
    }

    private void changeInt(int i){
        i=10;
    }
    private void  changeMyClass(MyClass m){
        m.i=10;
    }
    public class MyClass{
        int i=0; }}Copy the code

Results of the run:

2. Why does this happen?

As mentioned in the initial summary, only objects that need to be new will do so. This is because when new is performed, a space is created in the JVM heap, and the address of the object is returned after it is created. Now, there’s another question, where do I put objects that don’t need new? If it’s an object in a method that’s on the stack, if it’s an object that’s already in the.class file, it’s in the constant pool. I pulled up a graph from another blog, and it looks something like this, and the new object is the instance.If it is stored on the stack, there is no reference, but it is used directly. In other words, the value in the stack is the local variable of the method.

3. An explanation of the code in Step 1

If you understand it, you can skip this step. If you don’t understand it, you can take a look.

Why can’t the value of Tv.A change? Because when changeInt is called, the data stored in tv.a is the value, not the address, and there is no reference, passing the value directly does not change the local variable in TV, only the local variable in changeInt.

Why can the value of tv.m be changed? Because when the changeMyClass method is called, the data stored in tv.m is the address of a new object that is stored in the heap, what changes in the method is the local variable of the object (tv.m in the heap) that was found at the address received by the method.

One extra point

The MyClass M object in the changeMyClass method is stored in the stack of the method, which is removed once the method terminates.

Shallow copy and deep copy

  1. Shallow copy

The object created by new is usually a shallow copy, meaning that only the address of the object is copied, and the object is shared. This is the default rule, and it is not required to implement a shallow copy. 2. Deep copy Deep copy refers to the ability to generate two identical objects. The two objects do not share the same address, but exist independently. In general, you can convert an object into a JSON format and then call new to create an identical object in the heap.

Four,

Having said all that, it’s a question of whether you’re passing an address or a value. To summarize, I’m going to refer to Pointers in C. In C, there are Pointers and non-pointer objects. Non-pointer objects, like basic data types in Java, can only pass values; Pointer objects, like new objects in Java, pass addresses. C language requires us to create an object when the object to set whether the pointer type, that is, whether to add a *, but in Java, Java will need to set this active operation to a default setting.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support

Shall not be reproduced without permission!