- 浏览: 131596 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
qq466862016:
不错的文章
JDK动态代理与CGLIB代理的对比 -
jinxiongyi:
你好,jpedal pdf转换图片的 画质,怎么提高。。我转 ...
介绍几款PDF转图片的开源工具 -
qqdwll:
转图片消耗的内存还是不小。 有时间得找找有没有更好的办法, 把 ...
介绍几款PDF转图片的开源工具 -
xiaoyao3857:
Thanks for your work!It's help ...
Keeping Eclipse running clean (转载) -
iceside:
图片讲解非常详细,说清了引用复制是怎么回事
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.
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.
发表评论
-
介绍几款PDF转图片的开源工具
2011-09-09 00:40 4493最近项目中有个需求需要把PDF转成一张图。经过调查,有 ... -
jadclipse(反编译Eclipse插件)
2011-07-19 19:13 1620Jad Java decompiler plugin for ... -
Java开发时候的内存溢出
2011-07-13 17:33 1159这里以tomcat环境为例, ... -
class loader
2011-07-08 17:23 0Because Class.getResource() eve ... -
Jakarta-Common-BeanUtils使用笔记
2011-07-06 16:55 1302原文转发http://blog.csdn.net/fa ... -
基于MVC模式Struts框架研究
2011-04-13 20:02 1299不做web开发多年了, 可偶尔去面试的时候, 还是 ... -
Java反射与动态代理
2011-04-13 15:08 938这篇文章是 成富 先生在InfoQ上Java 深度历险系列的一 ... -
Java枚举类型
2011-04-04 19:50 754Tiger中的一个重要新特性是枚举构造,它是一种新的Java枚 ... -
Java 值传递的终极解释
2011-03-21 22:49 1940对于Java的值传递, 你真的了解么? Ja ... -
六种异常处理的陋习
2011-03-20 03:21 732你觉得自己是一个Java专 ... -
数组初始化
2011-03-20 02:40 835数组初始化,你觉得简单吗? a.如果你觉得简单,那请看下面的 ... -
Java 实现 hashCode 方法
2011-03-11 17:07 1131原文 http://www.javapractices.com ... -
Java 中 immutable class 以及怎样实现immutable 类
2011-03-11 16:47 1330原文 http://www.javapractices.com ... -
Java 内部类介绍
2011-02-16 17:14 948转载: http://zhidao.baidu.com/que ... -
Java 中的Clone 学习总结
2011-01-25 18:22 26961. 一个类需要实现clone. 一个最佳实践是它需要实现 C ... -
java 通过流, nio 移动文件或者文件夹
2011-01-04 17:54 1814我们用例子说明java怎样通过不同的方式移动文件或文件夹。 ... -
转 深入探讨SOAP、RPC和RMI
2010-12-17 00:34 1009这篇文章是从网上转下来的。 原文应该是写于2001年。 10 ... -
java 6 中的性能优化
2010-12-07 15:30 1408文章转载自: http://www ... -
创建强健,稳定的 JMS 系统
2010-12-07 15:21 949The most reliable way to produc ... -
Java Modifier Summary
2010-11-12 15:10 844<tbody> <tr> ...
相关推荐
java 的序列化与反序列化举例测试
java->serializable深入了解 java->serializable深入了解 java->serializable深入了解
java 序列化 对象 Serializable 写着玩的Demo 简单 实用
主要介绍了Java Serializable和Parcelable详解,并附实例代码的相关资料,需要的朋友可以参考下
Java_Serializable(序列化) 的理解和总结
java 序列化的问题 如何认识和解决序列化 java serializable
java.io.Serializable序列化问题
主要介绍了Java对象Serializable接口实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java序列化(Serializable)的作用和反序列化.doc 有详细的讲解哦。 在什么地方用的到都有说明的.
java 将对象序列化 输出对象的值,不懂可以百度序列化干啥的,为什么要用序列化,好处。
序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例...虽然你可以用你自己的各种各样的方法来保 存object states,但是Java给你提供一种应该比你自己好的保存对象状态的机制,那就是序列化。
說明如何將Serializable物件轉成stream
主要为大家详细介绍了java中Serializable接口作用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java 中 Serializable的应用,序列化的作用说明
Java序列化(Serializable)与反序列化_.docx
主要介绍了JAVA序列化Serializable及Externalizable区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java序列化(Serializable)的作用和反序列化.pdf
Java序列化(Serializable)与反序列化__1.docx
java 序列化对象 serializable 读写数据的实例,需要的朋友可以参考一下