Java "Aliases" and the clone mechanism

Source: Internet
Author: User
Tags aliases constructor copy reference
What is an alias? Use a simple example to illustrate the public class aliases{int i, public aliases () {I=1}, public aliases (int i) {this.i=i;} public static void N (String args[]) {aliases a=new aliases (); Aliases B=a; A and B point to the same object, A and B are aliases System.out.println ("A.i and B.I:" +a.i+ "" +b.i); System.out.println ("Add B:"); b.i++; System.out.println ("A.i and B.I:" +a.i+ "" +B.I);} Output: A.I and b.i:1 1 increased b:a.i and B.i:2 2 Obviously, a and B point to the same object, b=a this operation simply copies a reference to B, and the object is not copied. Java is a rerference to manipulate objects, above is an example of an explicit alias, when you pass the object to the function will also occur alias, as follows: public class aliases{int i; public aliases () {i=1; liases (int i) {this.i=i} public Increment (aliases as) {as.i++;} public static void Main (String args[]) {aliases A=ne W aliases (); System.out.println ("A.I before Increment:" +A.I); Increment (A); System.out.println ("A.i after Increment:" +A.I);} You can see a change in the value of I after a call to function increment (). In some cases, you may not want the incoming object to change, so you want the object within the function to be just a copy of the incoming object, and the change to that copy will not affect the original object. We know that C + + is by declaring a const argument, which means that this parameter is immutable, but don't forget that C + + has a so-called copy constructor, so the object in the function is indeed a copy, and Java does not support the copy constructor, because it is obvious that Java passes the reference to the object,Even if you copy it is just a copy of the reference (so some people say that Java is essentially only passing values). So there's no way out? Yes, that is "cloning mechanism", in the root class object has defined the Clone () method, all you have to do is implement the Cloneable interface, and overwrite the Clone () method, typical application of the following class Cloneclass implements cloneable{public int aint;public Object clone () {Cloneclass o = Null;try{o = (cloneclass) super.clone ();} catch (Clonenotsupportedexception e) {e.printstacktrace ();} return o;} Call the Super.clone () method, which automates the storage allocation and replication operations for you, thus enabling a deep copy of the object. We know that the same serilization can also achieve deep copies of objects Ah, why not use this? The root cause is a huge difference in efficiency, and although the clone () seems complicated at first, it's not as resource-consuming as the object's reading and writing. With the clone mechanism, you can create a copy of an object within the method call, which is localized, and does not affect the state of the original object for any of its operations. I personally believe that this is important for writing a large, secure program.

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.