Thoughts on Java Middle Communication Value and Establishment and Its Extension Depth Cloning

zhaozj2021-02-16  58

Everyone knows that there is no pointer in Java. Does Java really do not have a pointer? What is the handle? Where is the variable address? If there is no address, you can't imagine!

There are two ways to allocate memory in Java, one is allocated in the heap, one is allocated in the stack, all new objects are allocated in the stack, the passage of the parameters in the function is allocated in the stack . Normally, the memory can be large, such as the virtual memory in the 32-bit operating system can be used by the stack (when the memory is tight, even the hard disk can be a stack of storage space), and the memory allocation of the stack is limited . This is similar to the memory allocation in C (there is still another way in C for global variables or the memory allocation of local static variables, this is not said). There are several basic types of basic types such as int, float, double, char, byte. They are not objects, except that everything is object, all objects are allocated on the heap. What is the array of objects in Java, and the C is similar, which is a handle array or a pointer array, saved inside the address of each element. Unlike C , Java does not have operator overload and copy constructor (if you don't know these), therefore when you create objects or assign values ​​that have been created (note is object, not basic type): Object a = new Object When Object A = B (b is the subtype or same type of Object), the passage of the object address is performed and copied. This is the transfer and assignment of the handle. The handle is stored is the address of the object. The handle is a pointer, but it is only the address you can't get. Java is hidden by this clever will hide the pointer. When the object is transmitted to the method as a parameter, the transmitted is the address of the object, and the row parameter is saved is a copy of the address of the address (this is the most critical place, and the value transfer is the value of the argument. Copy as a row parameter) such as:

Public class example {INT i = 0;} public class a {public int i = 0; public example add0 (example e) {E.I ; returnif;} public void add1 (example e) {E.I ;} public Void Modify0 (Example E) {EXAMPLE B = E; // Assign the address of the E-line parameter object to the handle B.i ; // also modified the value of EI and the actual parameters} public void modify1 (Example E) { E = new example (); E.I ;} public static void main (string [] args) {example EX = new example (); a a = new a (); a = a.add0 (ex); // Equivalent to A. ADD0 (EX), no return value, because by passing the object address (handle), the value A.Add1 (ex); // add0, and add1 are directly modified by the transmitted object address (handle); The value of EX.I is directly modified, so the return value of the ADD0 is a bit A.MODIFY0 (EX); // The impact generated by EX is with add1 a.modify1 (ex); // does not have any effect on EX ( And this is equivalent to anything.

转载请注明原文地址:https://www.9cbs.com/read-22925.html

New Post(0)