Cloning of Java

Source: Internet
Author: User

Java has a deep copy and a shallow copy of the difference.

Shallow copy: He refers to the copy of the object, just copy the object itself (including the object's basic data class variables), does not copy the reference data type of the variable, that is, the copy of the new object basic data type value is unchanged, the value of the reference data type has changed. The new object also holds references to all objects maintained by the original object. That is, the A object (original), he has B and C objects, and an int a=10, copied out, became A1 object, he also holds B1 and C1 references, as well as int a1=10, but b!=b1,c!=c1. A shallow copy invokes the Clone method of object. And this object is to implement cloneable, otherwise it throws java.lang.CloneNotSupportedException exception.

Deep copy: He refers not only to copying the object itself, but also to copying references to objects maintained in the object, that is, the b==b1,c==c1 above, the reference to the object is also copied, and the basic data type.

First the object is to implement the serializable interface. Serialization. A deep copy can be written into a binary byte stream and then read from the binary byte stream, or the object is written to a file and read from the file.

1. How to File

/**
* Read files
* @param file
*/
public static void Readobjectbyfile (File file,student s) {

try {
ObjectInputStream ois = new ObjectInputStream (new FileInputStream (file));
try {
Person person = (person) ois.readobject ();
if (person!=null)
System.out.println (Person.getname ());
System.out.println (Person.gets () ==s);//true
Ois.close ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("File not found exception");
} catch (IOException e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Input and output exception");
E.printstacktrace ();
}
}

Writing an object to a file binary data
public static File Writefilebyobject (Object obj) throws ioexception{
File File = new file ("Obj.txt");

FileOutputStream fos = new FileOutputStream (file);
Building a byte output stream
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Building an object output stream
ObjectOutputStream out = new ObjectOutputStream (BOS);

Out.writeobject (obj);

byte[] datas = Bos.tobytearray ();

Fos.write (datas);

Fos.close ();

Out.close ();

return file;
}

2. Binary byte stream the way there are some exceptions, you can throw them out.

Person p = new person ("boy", 18, "Shenzhen Baoan");

Building a byte output stream
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Building an object output stream
ObjectOutputStream out = new ObjectOutputStream (BOS);
Write an object into a byte stream
Out.writeobject (P);
Convert an object to a byte
byte[] ByteArray = Bos.tobytearray ();
Read the converted bytes of an object
ObjectInputStream in = new ObjectInputStream (new Bytearrayinputstream (ByteArray));
try {
Get objects
Person person = (person) in.readobject ();
System.out.println (person==p);
System.out.println (P.gets () ==person.gets ());//true
System.out.println (Person.getage ());
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
Out.close ();
In.close ();


public class person implements serializable{

/**
*
*/
Private static final long serialversionuid = 1L;
private String name;
private int age;
Private String address;

Private Student S;

Public person (string name, int age, string address) {
THIS.name = name;
This.age = age;
this.address = address;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String getaddress () {
return address;
}
public void setaddress (String address) {
this.address = address;
}
Public Student GetS () {
return s;
}
public void Sets (Student s) {
THIS.S = s;
}

}

Cloning of Java

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.