Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Java serialization

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is Java serialization". In daily operation, I believe many people have doubts about what Java serialization is. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is Java serialization?" Next, please follow the editor to study!

Java serialization

For an object that exists in a Java virtual machine, its internal state is simply stored in memory. After JVM exits, memory resources are freed and the internal state of the Java object is lost. In many cases, the internal state of the object needs to be persisted, so the running object state is saved (the most direct way is to save it to the file system) and can be restored when needed, even if the Java virtual machine exits.

Object serialization mechanism is a kind of object persistence built in Java, which can easily realize the conversion between active objects and byte arrays (streams) in JVM. Java objects can be stored and transmitted by the network. Objects are serialized into byte streams at one end of the network, and transferred to the other end of the network, which can be restored from the byte stream to the objects in the running state of the Java virtual machine.

1. Related interface

Serialization of objects in the Java class is done through ObjectOutputStream and ObjectInputStream.

Java code

ObjectOutputStream (OutputStream out); void writeObject (Object obj); / / writes the non-transient, non-static attributes of the specified object to ObjectOutputStream ObjectInputStream (InputStream in); Object readObject (); / / reads the restore object information from the specified stream

You can only use the readObject () | writeObject () method to read and write objects. In addition to objects, basic types and arrays in Java can also be serialized, and for basic types, you can use interfaces like readInt (), writeInt (), readDouble (), writeDouble (), and so on for reading and writing.

2.Serializable interface

For any object that needs to be serialized, the interface Serializable must be implemented. It is only an identification interface, which does not have any members. It is only used to identify the object that indicates that the current implementation class can be serialized.

3.transient keyword

If some properties in the class want not to be serialized during object serialization, use the keyword transient annotation to modify it. Member properties labeled transient are skipped automatically when the object is serialized.

Need to pay attention to 4.Java serialization

(1)。 When an object is serialized, only the non-static member variables of the object are saved, not any member methods, static member variables and transient annotated member variables. (2)。 If the member variable of an object is an object, the data members of the object will also be saved and restored, and will be recursive. (3)。 If a serializable object contains a reference to a non-serializable object, the entire serialization operation will fail and a NotSerializableException will be thrown. You can mark this reference as transient, and the object can still be serialized.

5. A comprehensive instance class Student implements Serializable {private String name; private transient int age; private Course course; public void setCourse (Course course) {this.course = course;} public Course getCourse () {return course;} public Student (String name, int age) {this.name = name; this.age = age } public String toString () {return "Student Object name:" + this.name+ "age:" + this.age;}} class Course implements Serializable {private static String courseName; private int credit; public Course (String courseName, int credit) {this.courseName = courseName; this.credit = credit } public String toString () {return "Course Object courseName:" + courseName + "credit:" + credit;}}

Write objects to a file, serialize

Public class TestWriteObject {public static void main (String [] args) {String filePath = "C://obj.txt"; ObjectOutputStream objOutput = null; Course C1 = new Course ("C language", 3); Course c2 = new Course ("OS", 4); Student S1 = new Student ("king", 25); s1.setCourse (C1) Student S2 = new Student ("jason", 23); s2.setCourse (c2); try {objOutput = new ObjectOutputStream (new FileOutputStream (filePath)); objOutput.writeObject (S1); objOutput.writeObject (S2); objOutput.writeInt (123);} catch (FileNotFoundException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} finally {try {objOutput.close ();} catch (IOException e) {e.printStackTrace ();}} System.out.println ("Info: object written" + filePath) }

Read objects from a file and deserialize

Public class TestReadObject {public static void main (String [] args) {String filePath = "C://obj.txt"; ObjectInputStream objInput = null; Student S1 = null,s2 = null; int intVal = 0; try {objInput = new ObjectInputStream (new FileInputStream (filePath)); S1 = (Student) objInput.readObject () S2 = (Student) objInput.readObject (); intVal = objInput.readInt ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} catch (ClassNotFoundException cnfe) {cnfe.printStackTrace () } finally {try {objInput.close ();} catch (IOException e) {e.printStackTrace ();}} System.out.println ("Info: file" + filePath+ "read objects"); System.out.println (S1) System.out.println (s1.getCourse ()); System.out.println (S2); System.out.println (s2.getCourse ()); System.out.println (intVal);}}

Output:

[TestWriteObject] Info: object is written to C://obj.txt

[TestReadObjec] Info: read object Info in file C://obj.txt: read object Student Object name:king age:0 Course Object courseName:null credit:3 Student Object name:jason age:0 Course Object courseName:null credit:4 123 in file C://obj.txt

You can see that after the age attribute in Person is marked as transient, the age property is not serialized when serializing the object; after the name attribute in Course is static, the name static property of Course is not serialized; although the Person object is serialized, the Course object referenced by Person is also initialized.

At this point, the study of "what is Java serialization is over" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report