In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of the Serializable interface in Java? many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
First, let's take a look at what's in the Serializable interface. There is nothing in this interface, so how do you convert Java objects into byte arrays?
Public interface Serializable {}
Let's practice and see how Serializable serializes Java objects. Create a class SClass (serialization class), add two properties name and age, and create Getter and Setter methods.
Public class SClass {private String name; private Integer age; public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;}}
Creating a test class to write a SClass object to a file through ObjectOutputStream is actually a serialization process, and then reading the SClass object through ObjectInputSream is actually a deserialization process.
Public class Test {public static void main (String [] args) {/ / initialize SClass sclass = new SClass (); sclass.setName (Wang er); sclass.setAge (18); System.out.println (sclass); / / write objects to a file try (ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("file"));) {oos.writeObject (sclass) } catch (IOException e) {e.printStackTrace ();} / / read the object try from the file (ObjectInputStream ois = new ObjectInputStream (new FileInputStream (new File ("file"));) {SClass sclass1 = (SClass) ois.readObject (); System.out.println (sclass1);} catch (IOException | ClassNotFoundException e) {e.printStackTrace () }}}
Because SClass does not implement the Serializable interface, the system will report an error.
Following the stack information, let's take a look at ObjectOutputStream's writeObject0 () method. Some of the source codes are as follows:
If (obj instanceof String) {writeString ((String) obj, unshared);} else if (cl.isArray ()) {writeArray (obj, desc, unshared);} else if (obj instanceof Enum) {writeEnum ((Enum) obj, desc, unshared);} else if (obj instanceof Serializable) {writeOrdinaryObject (obj, desc, unshared);} else {if (extendedDebugInfo) {throw new NotSerializableException (cl.getName () + "\ n" + debugInfoStack.toString ()) } else {throw new NotSerializableException (cl.getName ());}}
This code means that when serializing, ObjectOutPutStream determines the type of object, and throws NotSerializableException if it is not a string, array, enumeration, or Serializable.
However, if SClass implements the Serializable interface, it can be serialized and deserialized.
How exactly is it serialized?
Take ObjectOutputStream as an example, it calls writeObject () → writeObject0 () → writeOrdinaryObject () → writeSerialData () → invokeWriteObject () → defaultWriteFields () when serializing.
The defaultWriteFields method is the interface that actually serializes the object.
So how do you deserialize?
Take ObjectInputStream as an example. When deserializing, it calls readObject () → readObject0 () → readOrdinaryObject () → readSerialData () → defaultReadFields ().
The defaultReadFields method is the interface that actually deserializes the object.
So the Serializable interface simply acts as an identifier, telling the program that it can be serialized.
Other points of knowledge:
1. Fields decorated by static and transient are not serialized.
Because serialization holds the state of the object, and static-decorated fields belong to the state of the class, it can be proved that serialization does not save static-decorated fields.
Transient, which means "temporary" in Chinese (on the importance of English), prevents the field from being serialized into the file. After being deserialized, the value of the transient field is set to the initial value, such as the initial value of the int type is 0 and the initial value of the object type is null.
two。 In addition to Serializable, Java provides a serialization interface, Externalizable (which is a bit of a mouthful to read).
3. SerialVersionUID is called serialized ID, and it is an important factor that determines whether Java objects can be deserialized successfully. During deserialization, the Java virtual machine compares the serialVersionUID in the byte stream with the serialVersionUID in the serialized class, and if the same, it can be deserialized, otherwise an exception with an inconsistent serialization version will be thrown.
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.