Analysis of Four Common Problems in Java serialization

Source: Internet
Author: User

1. What are the methods for implementing object serialization?

Java serialization can be implemented in two ways. One is to implement the serializable interface when the class is defined, and the other is to implement the externalizable interface. Objects of the classes that implement these two interfaces can be converted to throttling or restored from the byte stream without adding any code to the class. In rare cases, custom code is required to save or restore the object state.

Generally, you can use the serializable interface to implement serialization, because this interface does not need to implement any methods. The externalizable interface defines the writeexternal () and readexternal () methods, the classes that implement this interface must implement these two methods.

Ii. How to customize the serialization and deserialization processes?

In the process of Object serialization, if some sensitive information is involved, such as the bank card account and password, you need to consider confidentiality and how to prevent leakage during the transfer process. The transient keyword can be used to protect these attributes, but these attributes will not participate in the serialization process. In this case, you can consider Displaying Custom serialization and deserialization processes, adding encryption during serialization, and adding decryption during deserialization, rather than using the default serialization method.

Objects are implemented by calling the internal writeobject () and readobject () methods during serialization and deserialization. The writeobject () and readobject () methods are defined as follows:

private void writeObject(ObjectOutputStream out) throws IOExceptionprivate void readObject(ObjectInputStream in) throws IOException

Note: To prevent the default writeobject () and readobject () methods from being called during Object serialization, you must set these two methods to private.

If the custom serialization and deserialization processes are displayed, you only need to override the above two methods. To use the default serialization mechanism in the writeobject () method, you only need to call out in the writeobject () method. defaultwriteobject () method. Similarly, when the readobject () method uses the default serialization mechanism, you only need to call in. defaultreadobject () method.

In the process of custom serialization and deserialization, the default writeint (INT intvalue) and readint (INT intvalue) must be used for simple type attributes) read and Write operations on simple types. For attributes of the reference type, you can use readobject () and writeobject () in writeobject (objectoutputstream out) and readobject (objectinputstream in () method.

In addition, when reading attributes in a file or network, the read order must be consistent with the write order.

3. What is the role of adding the serialversionuid attribute to the serialization class?

Serialversionuid is a Private Static final attribute used to indicate compatibility between different versions of classes. This attribute is not required. When a class implements Java. io. serializable interface, but it does not display the serialversionuid attribute defining a long type. the Java serialization mechanism will automatically generate a serialversionuid according to the compiled class as the serialization version ID of the class, only the class generated by the same compilation will generate the same serialversionuid. During deserialization, JVM compares the serialversionuid In the byte stream with the serialversionuid of the local class. if the version is the same, the version can be deserialized, otherwise, an invalidclassexception exception occurs. If you do not want to forcibly divide the software version by compiling, that is, the class implementing the serialization interface is compatible with the previous version, you only need to explicitly define a final attribute named serialversionuid and the type is long, the same version number is guaranteed, and the invalidclassexception exception does not occur during serialization or deserialization.

4. How to correctly process Object serialization when serialization encounters inheritance?

During the serialization process, if the parent class implements the serializable and externalizable interfaces, all its subclasses will be automatically serialized.

However, in actual development, this is not desirable. Because the parent class cannot force all its child classes to be serialized.

If the parent class is not serialized, but the subclass implements the serializable interface, the subclass object cannot be serialized. Otherwise, an invalidclassexception exception is thrown.

The following provides a feasible method for your reference:

First, compile a subclass that can implement serialization. First, serialize itself.

Second, provide a construction method without parameters for the parent class and serialize the attributes of the parent class.

Public abstract class abssuperclass {protected int supervalue;/*** Construction Method of the parent class without parameters */Public abssuperclass () {} public abssuperclass (INT supervalue) {This. supervalue = supervalue;} public int getsupervalue () {return supervalue;} public void setsupervalue (INT supervalue) {This. supervalue = supervalue ;}@ overridepublic string tostring () {return "supervalue =" + supervalue ;}} /*** define the subclass and display the definition serialization and deserialization process * @ author gsucbiao ** @ date 2011-9-7 & 12:23:35 am */public class subclass extends abssuperclass implements serializable {Private Static final long serialversionuid = 1l; private int subvalue; Public subclass (INT supervalue, int subvalue) {super (supervalue); this. subvalue = subvalue;} private void writeobject (objectoutputstream out) throws ioexception {out. defaultwriteobject (); out. writeint (supervalue);} private void readobject (objectinputstream in) throws ioexception, classnotfoundexception {In. defaultreadobject (); supervalue = in. readint () ;}@ overridepublic string tostring () {return Super. tostring () + "subvalue =" + subvalue ;}}

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.