`
qqdwll
  • 浏览: 136753 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java Serializable

    博客分类:
  • Java
阅读更多
  Java 的 Serialazable 虽然只要要在类上加个申明, 类就可以被Serializable了。 但其实, 它并不是想象中的这么简单。 他会有很多的问题, 这是应为object被Serialaized 之后,可以认为是输出的API了。  那么, 对这个类的修改, 就会产生很多问题。 比如, 删除某些fields, 都可能导致deserializing失败。 下面给出一些系列化要注意的地方。

1.  Serial version UIDs.
Every serializable
class has a unique identification number associated with it. If you do not specify
the identification number explicitly by declaring a private static final long field named
serialVersionUID, the system automatically generates it by applying a complex
deterministic procedure to the class. The automatically generated value is affected by
the class's name, the names of the interfaces it implements, and all of its public and protected
members. If you change any of these things in any way, for example,by adding a trivial convenience method, the automatically generated serial version UID changes. If you fail to
declare an explicit serial version UID, compatibility will be broken.

2.  Do not accept the default serialized form without first considering whether it is
appropriate. Accepting the default serialized form should be a conscious decision on your
part that this encoding is reasonable from the standpoint of flexibility, performance, and
correctness. Generally speaking, you should accept the default serialized form only if it is
largely identical to the encoding that you would choose if you were designing a custom
serialized form.
The default serialized form of an object is a reasonably efficient encoding of the physical
representation of the object graph rooted at the object. In other words, it describes the data
contained in the object and in every object that is reachable from this object. It also describes
the topology by which all of these objects are interlinked. The ideal serialized form of an
object contains only the logical data represented by the object. It is independent of the
physical representation.
The default serialized form is likely to be appropriate if an object's physical
representation is identical to its logical content. For example, the default serialized form
would be reasonable for the following class, which represents a person's name:
//Good candidate for default serialized form
public class Name implements Serializable {
/**
* Last name. Must be non-null.
* @serial
*/
private String lastName;
/**
* First name. Must be non-null.
* @serial
*/
private String firstName;
/**
* Middle initial, or '\u0000' if name lacks middle initial.
* @serial
*/
private char middleInitial;
... // Remainder omitted
}
Logically speaking, a name consists of two strings that represent a last name and first name
and a character that represents a middle initial. The instance fields in Name precisely mirror
this logical content.

Even if you decide that the default serialized form is appropriate, you often must
provide a readObject method to ensure invariants and security.

The presence of the @serial tag tells the Javadoc utility to place this documentation on a
special page that documents serialized forms.


A reasonable serialized form for StringList is simply the number of strings in the list,
followed by the strings themselves. This constitutes the logical data represented by a
StringList, stripped of the details of its physical representation. Here is a revised version of
StringList containing writeObject and readObject methods implementing this serialized
form. As a reminder, the transient modifier indicates that an instance field is to be omitted
from a class's default serialized form:
//StringList with a reasonable custom serialized form
public class StringList implements Serializable {
private transient int size = 0;
private transient Entry head = null;
// No longer Serializable!
private static class Entry {
String data;
Entry next;
Entry previous;
}
// Appends the specified string to the list
public void add(String s) { ... }
/**
* Serialize this <tt>StringList</tt> instance.
*
* @serialData The size of the list (the number of strings
* it contains) is emitted (<tt>int</tt>), followed by all of
* its elements (each a <tt>String</tt>), in the proper
* sequence.
*/
private void writeObject(ObjectOutputStream s)
throws IOException {
s.defaultWriteObject();
s.writeInt(size);
// Write out all elements in the proper order.
for (Entry e = head; e != null; e = e.next)
s.writeObject(e.data);
}
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException {
s.defaultReadObject();
int size = s.readInt();
// Read in all elements and insert them in list
for (int i = 0; i < size; i++)
add((String)s.readObject());
}
... // Remainder omitted
}
Note that the writeObject method invokes defaultWriteObject and the readObject
method invokes defaultReadObject, even though all of StringList's fields are transient. If
all instance fields are transient, it is technically permissible to dispense with invoking
defaultWriteObject and defaultReadObject, but it is not recommended. Even if all
instance fields are transient, invoking defaultWriteObject affects the serialized form,
resulting in greatly enhanced flexibility. The resulting serialized form makes it possible to add
nontransient instance fields in a later release while preserving backward and forward
compatibility. If an instance is serialized in a later version and deserialized in an earlier
version, the added fields will be ignored. Had the earlier version's readObject method failed
to invoke defaultReadObject, the deserialization would fail with
a StreamCorruptedException.
Note that there is a documentation comment on the writeObject method, even though it is
private. This is analogous to the documentation comment on the private fields in the Name
class. This private method defines a public API, the serialized form, and that public API
should be documented. Like the @serial tag for fields, the @serialData tag for methods
tells the Javadoc utility to place this documentation on the serialized forms page.
分享到:
评论

相关推荐

    java serializable 序列化与反序列化

    `Serializable`接口是Java提供的一个标记接口,用于实现对象的序列化。当一个类实现了这个接口,它的实例就可以被序列化。 **一、Java序列化** 1. **什么是序列化**:序列化是将对象的状态(属性和成员变量)转换...

    java->serializable深入了解

    java-&gt;serializable深入了解 java-&gt;serializable深入了解 java-&gt;serializable深入了解

    Java_Serializable(序列化)的理解和总结

    ### Java Serializable(序列化)的理解和总结 #### 一、序列化的定义与目的 序列化是一种将对象的状态转换为可以存储或传输的形式的过程。在Java中,如果一个类实现了`Serializable`接口,那么该类的对象就可以被...

    java对象序列化Demo------------Serializable

    java 序列化 对象 Serializable 写着玩的Demo 简单 实用

    Java Serializable和Parcelable详解及实例代码

    `Serializable` 是Java标准库提供的一种简单的序列化方式,位于`java.io`包下。任何实现了`Serializable`接口的类,其对象都可以被序列化。实现这个接口不需要实现任何方法,只需要在类定义上添加`implements ...

    java 序列化的问题 如何认识和解决序列化 demo

    java 序列化的问题 如何认识和解决序列化 java serializable

    Java对象Serializable接口实现详解

    Java对象Serializable接口实现详解 Java中的Serializable接口是java.io包中定义的,用于实现Java类的序列化操作而提供的一个语义级别的接口。Serializable序列化接口没有任何方法或者字段,只是用于标识可序列化的...

    Serializable java序列号

    在Java中,如果一个类需要支持序列化,那么该类需要实现`java.io.Serializable`接口,虽然这个接口没有定义任何方法,但是它的存在作为一个标记,表明该类的对象可以被序列化。 序列化的优点主要有以下几点: 1. **...

    java.io.Serializable序列化问题

    ### Java.io.Serializable 序列化问题详解 #### 一、序列化的概念与作用 在 Java 编程语言中,序列化是一种将对象的状态(即成员变量的值)转换为可以存储或传输的形式的过程。通常,这种形式是字节流,但也可以是...

    java序列化(Serializable)的作用和反序列化.doc

    ### Java序列化(Serializable)的作用与反序列化详解 #### 一、序列化的概念 序列化是指将程序中的对象转换为一系列字节流的过程,主要用于保存对象的状态或在网络之间传输对象。序列化的主要目的是为了能够持久化...

    java序列化(Serializable)的作用和反序列化

    ### Java序列化(Serializable)的作用与反序列化详解 #### 一、序列化是什么? 序列化是指将程序中的对象转换为字节流的过程,从而方便存储或传输这些对象。通常,序列化用于将对象的状态(即其实例变量的值,而非...

    轉Serializable至Stream

    在Java编程中,`Serializable`接口是用于对象序列化的重要工具。对象序列化是指将一个对象的状态转换为字节流的过程,以便存储或通过网络进行传输。另一方面,`Stream`通常指的是I/O流,它是Java处理输入/输出数据的...

    java中Serializable接口作用详解

    Java 中的 Serializable 接口作用详解 Java 中的 Serializable 接口是一个非常重要的接口,它允许对象被序列化,换句话说,就是将对象转换成字节序列,以便于存储或通过网络传输。Serializable 接口的作用主要体现...

    dubbo介绍和使用

    - **高效版 Serializable**:Dubbo 对 Java Serializable 进行了优化,提高了序列化效率。 2. **传输**:Dubbo 支持多种传输协议,例如 Netty、Mina 和 XSocket 等。 - **Netty**:一个高性能的 NIO 框架,广泛...

    即时聊天(序列化)

    Java中,我们可以使用Java Serializable接口来标记一个类是可序列化的。序列化不仅可以用于网络传输,还可以用于持久化存储,便于恢复对象状态。例如,客户端发送的消息对象会被序列化成字节流,然后通过网络发送到...

    WebLech笔记

    1. Java Serializable(序列化)的理解和总结 序列化是一种机制,用于保存在内存中的对象状态(实例变量),并可以将保存的对象状态再读出来。Java提供了序列化机制,用于将对象状态保存到文件中或传输到网络中。 ...

    axis web services 例子

    4. **Java Serializable接口**:自定义类需要实现Serializable接口,以便能够被序列化和反序列化。 5. **XMLBeans或JAXB**:Apache Axis可以使用XMLBeans或JAXB(Java Architecture for XML Binding)进行对象到XML...

    Java 串行化(序列化)Serializable/Externalizable

    Java 串行化主要通过实现`java.io.Serializable`接口来实现,同时也提供了`java.io.Externalizable`接口来提供更细粒度的控制。 **一、Serializable接口** `Serializable`是Java中的一个标记接口,没有包含任何方法...

    序列化 serializable demo

    例如,`MySerializable.java`和`Product.java`两个文件可能分别代表实现了`Serializable`接口的类。`MySerializable`可能是自定义的一个示例类,而`Product`可能是表示产品的类,它们都包含了可序列化的属性。 在`...

Global site tag (gtag.js) - Google Analytics