In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Java serialization and cloning of the case analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Serialization
Java serialization technology allows you to write the state of an object to a Byte stream and read the data from the Byte stream elsewhere to reconstruct the same object.
When two processes are communicating remotely, they can send various types of data to each other. No matter what type of data it is, it will be transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object into a byte sequence before it can be transmitted over the network; the receiver needs to restore the byte sequence to the Java object.
The process of converting Java objects into byte sequences is called object serialization.
The process of restoring a byte sequence to a Java object is called deserialization of the object.
1. The purpose of serialization
With the serialization of objects, the current working state of the application can be saved, and the next time it is started, it will automatically return to the last executed state.
Object serialization has two main uses:
(a) save the byte sequence of the object to the hard disk, usually in a file
(B) the byte sequence of the object transmitted over the network.
2. Implementation of serialization
(1) Serialization API in JDK class library
Java.io.ObjectOutputStream represents the object output stream, and its writeObject (Object obj) method serializes the obj object specified by the parameter, writing the resulting byte sequence to a target output stream.
Java.io.ObjectInputStream represents an object input stream, and its readObject () method reads sequences of bytes from a source input stream, deserializes them into an object, and returns them.
Only objects of classes that implement the Serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the Serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, while the class that only implements the Serializable interface can use the default serialization mode.
(2) the process of object serialization and deserialization
The class that needs to be serialized implements the Serializable interface, which has no methods that need to be implemented, implements Serializable is just to mark that the object can be serialized, and then use an output stream (such as: FileOutputStream) to construct an ObjectOutputStream (object stream) object, then, using the writeObject (Object obj) method of the ObjectOutputStream object, you can write out the object whose parameter is obj (that is, save its state), and use the input stream to restore it.
Object serialization includes the following steps:
(a) create an object output stream that can wrap another type of target output stream, such as a file output stream
(B) write objects through the writeObject () method of the object output stream.
The steps for object deserialization are as follows:
(a) create an object input stream that can wrap another type of source input stream, such as a file input stream
(B) read the object through the readObject () method of the object input stream.
Let's take a look at a corresponding example. The content of the class is as follows:
Java code
Import java.io.*; import java.util.Date; public class ObjectSaver {public static void main (String [] args) throws Exception {ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("D:"objectFile.obj")); / / serialize object Customer customer = new Customer ("Amigo", 24) Out.writeObject ("Hello!"); out.writeObject (new Date ()); out.writeObject (customer); out.writeInt (123); / / write basic type data out.close () / / deserialize object ObjectInputStream in = new ObjectInputStream (new FileInputStream ("D:" objectFile.obj ")); System.out.println (" obj1= "+ (String) in.readObject ()); System.out.println (" obj2= "+ (Date) in.readObject ()) Customer obj3= (Customer) in.readObject (); System.out.println ("obj3=" + obj3); int obj4= in.readInt (); System.out.println ("obj4=" + obj4); in.close ();} class Customer implements Serializable {private String name Private int age; public Customer (String name, int age) {this.name = name; this.age = age;} public String toString () {return "name=" + name + ", age=" + age;}} import java.io.*; import java.util.Date Public class ObjectSaver {public static void main (String [] args) throws Exception {ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("D:"objectFile.obj")); / / serialize object Customer customer = new Customer ("Amigo", 24); out.writeObject ("Hello!"); out.writeObject (new Date ()); out.writeObject (customer); out.writeInt (123); / / write basic type data out.close () / / deserialize object ObjectInputStream in = new ObjectInputStream (new FileInputStream ("D:" objectFile.obj ")); System.out.println (" obj1= "+ (String) in.readObject ()); System.out.println (" obj2= "+ (Date) in.readObject ()); Customer obj3= (Customer) in.readObject (); System.out.println (" obj3= "+ obj3); int obj4= in.readInt (); System.out.println (" obj4= "+ obj4) In.close ();} class Customer implements Serializable {private String name; private int age; public Customer (String name, int age) {this.name = name; this.age = age;} public String toString () {return "name=" + name + ", age=" + age;}}
The output is as follows:
Java code
Hello, obj1=! Obj2=Sat Sep 26 22:02:21 CST 2010 obj3=name= Amigo, age=24 obj4=123 obj1= Hello! Obj2=Sat Sep 26 22:02:21 CST 2010 obj3=name= Amigo, age=24 obj4=123
Therefore, the example is relatively simple and will not be discussed in detail here.
3. SerialVersionUID function:
In order to maintain version compatibility when serialization, that is, deserialization still maintains the * * nature of the object when the version is upgraded.
There are two ways to generate:
One is the default 1L, such as:
Java code
Private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L
One is to generate a 64-bit hash field based on class name, interface name, member methods and properties, such as:
Java code
Private static final long serialVersionUID = xxxxL; private static final long serialVersionUID = xxxxL
II. Cloning
Sometimes you want to get a copy of the object, and the entity of the copy is a clone of the original object. The change of the replica entity will not cause the change of the original object entity, and such a replica is called the clone object of the original object entity or clone for short.
1. Shallow replication (shallow cloning)
Concept: all variables of the copied object contain the same value as the original object, while all references to other objects still point to the original object. In other words, shallow copy copies only the object under consideration, not the object it refers to.
Method: class implements Cloneable, then override the clone () method, and call super.clone () in the clone () method. There is no other operation.
2. Deep replication (deep cloning)
Concept: all variables of the copied object contain the same values as the original object, excluding those that refer to other objects. Variables that refer to other objects will point to the new objects that have been copied, rather than the original referenced objects. In other words, Deep copy copies all the objects referenced by the object to be copied.
Methods:
(1) Class implements Cloneable, then override the clone () method, call super.clone () in the clone () method, and then clone the object referred to by the referenced variable.
(2) Serialization: write the object out to the object output stream, then the object read back with the object input stream is a deep clone of the original object.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.