Deep understanding of java reference types

Please see the original address
http://www.cnblogs.com/SilentCode/p/4858790.html


In Java, types can be divided into two categories: value types and reference types. The value type is the basic data type (such as int, double, etc.), and the reference type refers to all types except the basic variable type (such as the type defined by class). All types will allocate a certain storage space in memory (the formal parameters will also allocate storage space when they are used, and this storage space will disappear automatically after the method call is completed), and the basic variable type has only one storage space (allocated in stack ), and reference types have two storage spaces (one in stack and one in heap).

Simple types are passed by value. A

reference is actually like the name or alias of an object, and an object will be stored in memory. Requests a block of space to hold data, which may take up varying amounts of space depending on the size of the object. When accessing an object, we do not directly access the data of the object in memory, but access it by reference. A reference is also a data type, we can think of it as something like a pointer in the C++ language, which indicates the address of an object in memory - but we can't observe what the address is.

If we define more than one reference to the same object, then these references are not the same, because the reference is also a data type and requires a certain memory space (stack, stack space) to save. But their values ​​are the same, both indicating the location of the same object in memory (heap, heap space).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326596528&siteId=291194637