Thinking about the cloning of the value and the depth of the extension in Java (plainly, Java can only pass object pointers)

Source: Internet
Author: User


Thoughts on the cloning of value and extension depth in Java

We all know there are no pointers in Java. Doesn't Java really have pointers? What is a handle? Where is the variable address? It would be unthinkable without an address!
There are two ways in which memory is allocated in Java, one is allocated in the heap, one is allocated on the stack, all new objects are allocated in the heap, and the arguments in the function are allocated in the stack. In general, heap memory can be very large, such as 32-bit operating system virtual memory can be used by the heap (when the memory is tight and even the hard disk can be a heap of storage space), and the memory allocation of the stack is limited.

This is almost the same as memory allocations in C + + (there is another way in C + + for global variables or for memory allocations for local static variables, which is not said). There are several basic types in Java, such as Int,float,double,char,byte, which are not objects, except that everything is an object, and all objects are allocated on the heap.

What is an array of objects in Java, similar to C + +, is a handle array or an array of pointers, which holds the address of each element. Unlike in C + +, Java does not have operator overloading and copy constructors (it doesn't matter if you don't understand them), so when you create an object or assign a value to an object that has already been created (note that the object is not the base type): Object A=new object and Object a= B (b is a subtype of object or the same type), the object address is passed and copied. This is the delivery and assignment of the said handle.

The handle is stored in the object's address, the handle is a pointer, but you can not get the address, Java is through this clever to hide the pointer. When an object is passed as a parameter to a method, the address of the object is passed, and the row parameter holds a copy of the address of the argument (this is the most critical place, also the value pass, and the value pass is the copy of the value of the argument as the row parameter)

Such as:

public class example{
int i=0;
}
public class a{
public int i=0;
Public Example add0 (Example e)
{
e.i++;
return e;
}

public void Add1 (Example e)
{
e.i++;
}

public void Modify0 (Example e)
{
Example b=e;//assigns the address of the E-line parameter object to handle B
b.i++;//also modifies the values of the E.I and arguments
}

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 is required because the value of I in ex is modified directly by passing the object address (handle)
A.ADD1 (ex);//add0,add1 changes the value of ex.i directly in the method body, so the return value of add0 is a bit superfluous
A.modify0 (ex);//The effect on EX is the same as Add1
A.modify1 (ex);//does not have any effect on EX (and that is equivalent to nothing).

This may make some people confused. Why is it? Because it is a copy of the object address "value passing", in Modify1 e=new Example (); in fact, E is simply a handle to the copy of the address of the ex object, when the E assignment is only the assignment of E in the stack (the variable e assigned to the copy of the Ex Pointer), Instead of changing the direction of the handle of the ex, when the method call finishes the stack pops up, E is going to be garbage collected without any use. Of course you can use it as a return value, which is another matter.
}
}

Here, if you can understand this principle, then you can write a reasonable and efficient program, and can avoid some potential logic errors, such as: The object has been changed in the method, you may not know! Remember that C + + at this point and Java is very different, C + + default is the value of the pass, the line participants by the bit copy argument (if the pointer or reference is similar to Java), in the method as a parameter to pass the object, Java is more like a C + + Pass the reference, of course, there is a difference, it is The reference in the object cannot be re-assigned to another object.

That is to say, the re-assignment in the modify1 is not possible with reference. If you don't know about C + +, then when I say nothing, compare it to C + + just to help better understand (for people who are familiar with C + + and unfamiliar with Java). I also know very little about C + +, and I usually focus on Java in my main work. So if any man finds out what's wrong with the above explanation, please enlighten me.

Extended to the cloning technology all objects in Java are subclasses of the object class, the object class defines the protected clone () method, which is the same as a bitwise copy in C + +, and therefore also brings if the object contains another object (note that the object is not a basic data type , a pointer to a primitive data type that will be copied directly (a handle in Java), and clone does not clone the contained object, but instead copies the handle or pointer of the contained object.

Therefore, it is not considered that the copied object can be modified arbitrarily, because it and the object being cloned contain the same object, which may cause potential conflict problems. As for the deep clone method, it is very simple to overwrite the Clone method in the parent class object class in the subclass to ensure that each contained object is clone by bit.

If the data contained are all basic type data, then nothing is done. Deep clone There is another way to use serializable, but variables that are modified by the transient keyword in an object are not serialized. Because there are not many places in which clone is used, there is not much to say. But when you meet, you must be careful.

Http://www.51cto.com/specbook/449/2869.htm

Thinking about the cloning of the value and the depth of the extension in Java (plainly, Java can only pass object pointers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.