Java.io.Serializable (serialization interface) __java-syntax

Source: Internet
Author: User
Tags object serialization serialization

The interface is defined as:

The public interface Serializable {}//curly braces is empty. This is a special kind of interface.

Java object serialization enables you to convert an object that implements the serializable interface into a byte stream, so that when you use the object later, you can recover the byte data and reconstruct the object accordingly.
1. Feature 1: When an object is serialized, only non-static member variables of the object (including variables declared private) are saved, and no member methods and static member variables can be saved.
2: If an object's member variable is an object, then the data member of the object is also serialized.
3: If a serializable object contains a reference to an object that is not serializable, the entire serialization operation will fail and a notserializableexception will be thrown. We can mark this reference as transient, and the object can still be serialized.
2. Implement this interface simply add implements java.io.Serializable to your class. The IDE prompts you to *add default serial version id* and then it's done.
**serialversionuid**
In simple terms, the Java serialization mechanism verifies version consistency by determining the serialversionuid of a class at run time. When deserialization occurs, the JVM compares the serialversionuid in the stream of bytes to the serialversionuid of the local Entity (Class), and if the same is considered consistent, it can be deserialized, otherwise there will be an exception that is inconsistent with the serialized version. InvalidCastException.
When a class implements the serializable interface and does not display the definition serialversionuid,eclipse will prompt you "the Serializable class Splitsentence does not declare a Static final Serialversionuid field of type Long. Click Quick Fixes available,eclipse will help you implement the following statement:
private static final long serialversionuid = 1L;
3.transient keyword

The Java serialization provides a mechanism for persisting object instances. When you persist an object, you may have a special object data member, and we do not want to use the serialization mechanism to save it. In order to turn off serialization on one domain of a particular object, you can precede this field with the keyword transient. When an object is serialized, the value of the transient variable is not included in the serialization representation, whereas transient variables are included. 4. Examples

A generic function:

/**
 * @param srcobject its class need to implement Serializable
 * *
/@SuppressWarnings ("unchecked")
public static <T> T Deepcopy (t srcobject) throws IOException, classnotfoundexception{
	bytearrayoutputstream Bytestream=new Bytearrayoutputstream ();
	ObjectOutputStream oos=new ObjectOutputStream (bytestream);
	Oos.writeobject (srcobject);
	ObjectInputStream ois = new ObjectInputStream (new Bytearrayinputstream (	Bytestream.tobytearray ()));
	Return (T) ois.readobject ();
}


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.