And the reference value is passed commonplace --Java transfer

Due

Two days before the interview was asked this question, though before long ago to find out about this issue, but did not understand, so the interview when all of a sudden panic, food is original sin, chicken dishes today to replenish the basics.

In fact, this issue has been discussed, the three common parlance, 1, Java value passed has passed by reference, 2, only the value is passed, only 3 passed by reference, today checked a lot of information, I found that this problem is not with casually can make it clear.

Let me talk about mass participation

The method can be divided into parameters and parameter arguments, argument refers to the actual value will be passed when it is called, before the method call has been initialized. Parameter refers to the process for "receiving" parameter arguments, it is effective In this method, i.e. scope. After the execution method ie destroyed.

OK, it went on to say the so-called value and reference.

Java data types

1, the basic data types, this is the smallest granularity in Java data types, including: byte, short, int, long, float, double, char, boolean.

2, reference types: a data type is stored in the programming language where the actual content of the address. Usually a class, an interface, an array.

 Here I have to say about the divided memory areas:

After a file in Java compiler, transformed into class files, after the JVM class files through the class loader loaded into the data and information related to run-time data area need to use the stored program is running.

Run-time data area

1, the virtual machine stack, which is private to the thread, the thread is so isolated, the stack is stored inside the stack frame, each stack frame showing a method is called, the process of method invocation stack frame corresponds to a push from stack process, the stack frame information is stored in the method required for the operation, which is used to support data structures and methods for performing the method calls:

(1), the local variable table (methods in the local variables, which is the basic type, the value is directly stored as a reference type, directed to a particular storage object reference)

(2), exit address method (method of performing the return address End)

(3), the operand stack (JVM referred to as stack-based execution engine)

(4), a reference point constant pool

(5), some additional information.

2, the heap is shared by all threads, used to store objects and arrays, in the JVM, only a heap.

3, the method area: one is shared by all threads of memory logic area, the JVM is only one way in this area, some threads may be used to store shared content, but it is also thread-safe. Information stored in the method area are:

Access modifiers full path name of the class, the class of the fully qualified name of the direct superclass, class, class type, an ordered list of the fully qualified name of the class of direct interface, the constant pools.

4, native method stacks: native method stacks similar to the virtual machine stack, the thread is private, except that, for the Java virtual machine stack is the method of service, and the local stack method is a method for the native service.

5, the program counter, the thread is private, the bytecode interpreter is working by changing the value in the program counter to select the next bytecode instruction execution cycle of the branch so that it needs to achieve.

How is the data stored?

JVM during operation, involving memory allocations are: heap, stack, static method area, constant area. Memory allocation strategies are: Stacking, stack, statically.

Storing basic data types:

1, the basic data types of local variables and his values ​​are stored in the stack frame on the stack, i.e. the stack of the virtual machine.

Such as: int a = 30; in fact this two-step process, the first step is to create an age variable, the second step is to find out whether there are 30 this value stack, and if so, pointing it directly to age, there is no then open up a memory to store 30.

Then age point to it. It re-assignment is the same token, such as age = 50, to find whether there is the stack 50, there is the point, then there is no space to open up a store 50, then age point to it. So enough to see, the basic data types assignment is not to change their data, as above, but first find the stack if there is, then there is to it, it does not open up a block of memory to hold the new value.

2, the basic data types of the member variables

The difference is that with the above, it is stored on the heap, because it is followed by an instance of an object, with the same instance of an object's life cycle.

3, the basic data type of static variable, and are different from the above, and its values ​​of static variables are stored in the runtime constant region, which is to follow the class loading, with the disappearance of Class disappear.

Reference data types of storage:

Reference data types is divided into two parts, a variable, the presence of the stack, the address for storing actual content, while there is a heap memory to keep the actual content.

The so-called value transfer and references

Value transfer

When the method is called, the arguments by the parameter copy its contents incoming internal method, the received parameter in this case is actually a copy of the argument, any method of operation of the body, is a copy operation It does not affect the argument. Such as:

public class Main {

    public static void main(String[] args)  {
        int a = 10;
        function(a);
        System.out.println(a);
    }
    private static void function(int a){
        a = 50;
    }
}

Output: 10

Bedding before binding, wherein the first JVM will be main () onto the stack virtual machine with a stack frame memory, is the current stack frame, the stack frame is stored in the local variable table method, operand stack, addresses, etc. for export. When After performing the function method, the JVM create create one of its
stack frame, used to store information about function, and thus the value of a is in the stack frame function in, and its value from the argument replication come, and when re-assignment method of the internal body, not to change the value of the stack frame is located in a main, but in the current stack frame, the values are modified. And between the stack frames are isolated, it will not affect the argument.

Passed by reference

And transmitting different reference values ​​transmitted in that, when calling the method, the argument address to the parameter by a method, the content will affect the operation of argument itself, but however, where there is need points. Such as:

public class Main {
    public static void main(String[] args)  {
        StringBuilder a = new StringBuilder("10");
        function(a);
        System.out.println(a);
    }
    private static void function(StringBuilder str){
        str.append("20");
    }
}

Output: 1020 

public class Main {
    public static void main(String[] args)  {
        StringBuilder a = new StringBuilder("10");
        function(a);
        System.out.println(a);
    }
    private static void function(StringBuilder str){
        str = new StringBuilder("20");
    }
}

Output: 10

This is why it, have said before, the variable is stored on the stack, the actual content stored in the stack, causing different reasons here is that the operation assignment in Java (=) will not change its content, but the first check there already exists and is equal to its value, there are points to

It does not then create another one, and then point to it, so in the second paragraph of code, str = new StringBuilder ( "20") does not modify the original content itself but points to a new object, which is the parameter str and the argument just a different address, there will be no effect on the parameters. In the first code, str.append (); at the memory is operated, i.e. while at the memory reference argument at the memory, so influential.

So I think there may be a summary, it is not passed by reference between Java, regardless of the base data type or reference type, pass all values ​​are copies of the argument, but the difference is that the basic data type is real reference copies of the content, but also a reference type, but the content is the address assignment only, that is, arguments and a reference point to the same memory (the object), whether or not parameter changes will change the argument depends on whether the operation is directed this memory be modified directly. If this assignment, the operating parameter of the argument has no effect.

 

Guess you like

Origin www.cnblogs.com/Yintianhao/p/12335584.html