Java Clone Learning Summary

Source: Internet
Author: User

Because everything in Java is a handle, and because each object is created in the memory heap-it is collected as garbage when it is no longer needed-the object's operation changes, especially when the object is passed and returned. For example, in C and C + +, if you want to initialize some storage space in a method, you may need to ask the user to pass the address of that storage area into the method. Otherwise, you must consider who is responsible for clearing the area. Therefore, the interfaces of these methods and their understanding of them are somewhat more complicated. In Java, however, there is no need to care who is responsible for purging or whether it still exists when an object is needed. Because the system will take care of everything for us. Our program can create an object when it is needed. and further, there is no need to worry about the details of the transmission mechanism of that object: simply passing the handle. Sometimes, this simplification is very valuable, but at other times it seems superfluous.
The shortcomings of this mechanism can be understood from two aspects:
(1) Be sure to pay an efficient loss for additional memory management (although the loss is small), and there is always a trace of uncertainty about the time it takes to run (because the garbage collector may be forced to act when there is not enough memory). For most applications, the advantages appear to be more important than the disadvantages, and some passages that are very demanding in time can be written in the native method (see Appendix A).
(2) Alias handling: Sometimes inadvertently get two handles to the same object. Problems can occur only if these two handles assume that they point to a "clear" object. The issue must be given sufficient attention. You should also "clone" an object as much as possible to prevent another handle from being affected by an unnecessary change. In addition, consider creating a "immutable" object so that its operations can return a new object of the same type or different types, thereby increasing the execution efficiency of the program. But don't change the original object so that you don't feel any change in any other aspect of that object's alias.

Some people think that Java cloning is a clumsy guy, so they implemented their own cloning scheme (annotation ⑤), never eliminate the call Object.clone () method, This eliminates the need to implement cloneable and capture clonenotsupportexception violations. This is reasonable, and because clone () is rarely supported in the Java standard Library, this is clearly a "safe" approach. As long as you do not invoke Object.clone (), you do not have to implement cloneable or catch the violation, so it seems acceptable.

⑤:doug Lea attaches particular importance to this problem and recommends it to me, saying that only a function named duplicate () can be created for each class.

An interesting keyword in Java is byvalue (by value), which belongs to one of those "reserved but not implemented" keywords. After understanding the alias and cloning issues, you can imagine that byvalue will eventually be used in Java to implement an automated local copy. This solves more complex cloning problems and makes the code written in this case simpler and more robust.

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.