`
中国爪哇程序员
  • 浏览: 165021 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

序列化(一)英文API

    博客分类:
  • java
阅读更多
JDK Serializable 描述

/**
* Serializability of a class is enabled by the class implementing the
* java.io.Serializable interface. Classes that do not implement this
* interface will not have any of their state serialized or
* deserialized.  All subtypes of a serializable class are themselves
* serializable.  The serialization interface has no methods or fields
* and serves only to identify the semantics of being serializable. <p>
*
* To allow subtypes of non-serializable classes to be serialized, the
* subtype may assume responsibility for saving and restoring the
* state of the supertype's public, protected, and (if accessible)
* package fields.  The subtype may assume this responsibility only if
* the class it extends has an accessible no-arg constructor to
* initialize the class's state.  It is an error to declare a class
* Serializable if this is not the case.  The error will be detected at
* runtime. <p>
*
* During deserialization, the fields of non-serializable classes will
* be initialized using the public or protected no-arg constructor of
* the class.  A no-arg constructor must be accessible to the subclass
* that is serializable.  The fields of serializable subclasses will
* be restored from the stream. <p>
*
* When traversing a graph, an object may be encountered that does not
* support the Serializable interface. In this case the
* NotSerializableException will be thrown and will identify the class
* of the non-serializable object. <p>
*
* Classes that require special handling during the serialization and
* deserialization process must implement special methods with these exact
* signatures: <p>
*
* <PRE>
* private void writeObject(java.io.ObjectOutputStream out)
*     throws IOException
* private void readObject(java.io.ObjectInputStream in)
*     throws IOException, ClassNotFoundException;
* private void readObjectNoData()
*     throws ObjectStreamException;
* </PRE>
*
* <p>The writeObject method is responsible for writing the state of the
* object for its particular class so that the corresponding
* readObject method can restore it.  The default mechanism for saving
* the Object's fields can be invoked by calling
* out.defaultWriteObject. The method does not need to concern
* itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput.
*
* <p>The readObject method is responsible for reading from the stream and
* restoring the classes fields. It may call in.defaultReadObject to invoke
* the default mechanism for restoring the object's non-static and
* non-transient fields.  The defaultReadObject method uses information in
* the stream to assign the fields of the object saved in the stream with the
* correspondingly named fields in the current object.  This handles the case
* when the class has evolved to add new fields. The method does not need to
* concern itself with the state belonging to its superclasses or subclasses.
* State is saved by writing the individual fields to the
* ObjectOutputStream using the writeObject method or by using the
* methods for primitive data types supported by DataOutput.
*
* <p>The readObjectNoData method is responsible for initializing the state of
* the object for its particular class in the event that the serialization
* stream does not list the given class as a superclass of the object being
* deserialized.  This may occur in cases where the receiving party uses a
* different version of the deserialized instance's class than the sending
* party, and the receiver's version extends classes that are not extended by
* the sender's version.  This may also occur if the serialization stream has
* been tampered; hence, readObjectNoData is useful for initializing
* deserialized objects properly despite a "hostile" or incomplete source
* stream.
*
* <p>Serializable classes that need to designate an alternative object to be
* used when writing an object to the stream should implement this
* special method with the exact signature: <p>
*
* <PRE>
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
* </PRE><p>
*
* This writeReplace method is invoked by serialization if the method
* exists and it would be accessible from a method defined within the
* class of the object being serialized. Thus, the method can have private,
* protected and package-private access. Subclass access to this method
* follows java accessibility rules. <p>
*
* Classes that need to designate a replacement when an instance of it
* is read from the stream should implement this special method with the
* exact signature.<p>
*
* <PRE>
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
* </PRE><p>
*
* This readResolve method follows the same invocation rules and
* accessibility rules as writeReplace.<p>
*
* The serialization runtime associates with each serializable class a version
* number, called a serialVersionUID, which is used during deserialization to
* verify that the sender and receiver of a serialized object have loaded
* classes for that object that are compatible with respect to serialization.
* If the receiver has loaded a class for the object that has a different
* serialVersionUID than that of the corresponding sender's class, then
* deserialization will result in an {@link InvalidClassException}.  A
* serializable class can declare its own serialVersionUID explicitly by
* declaring a field named <code>"serialVersionUID"</code> that must be static,
* final, and of type <code>long</code>:<p>
*
* <PRE>
* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
* </PRE>
*
* If a serializable class does not explicitly declare a serialVersionUID, then
* the serialization runtime will calculate a default serialVersionUID value
* for that class based on various aspects of the class, as described in the
* Java(TM) Object Serialization Specification.  However, it is <em>strongly
* recommended</em> that all serializable classes explicitly declare
* serialVersionUID values, since the default serialVersionUID computation is
* highly sensitive to class details that may vary depending on compiler
* implementations, and can thus result in unexpected
* <code>InvalidClassException</code>s during deserialization.  Therefore, to
* guarantee a consistent serialVersionUID value across different java compiler
* implementations, a serializable class must declare an explicit
* serialVersionUID value.  It is also strongly advised that explicit
* serialVersionUID declarations use the <code>private</code> modifier where
* possible, since such declarations apply only to the immediately declaring
* class--serialVersionUID fields are not useful as inherited members. Array
* classes cannot declare an explicit serialVersionUID, so they always have
* the default computed value, but the requirement for matching
* serialVersionUID values is waived for array classes.
*
* @author  unascribed
* @version 1.25, 11/17/05
* @see java.io.ObjectOutputStream
* @see java.io.ObjectInputStream
* @see java.io.ObjectOutput
* @see java.io.ObjectInput
* @see java.io.Externalizable
* @since   JDK1.1
分享到:
评论

相关推荐

    akka-protobuf_2.11-2.4.20-API文档-中英对照版.zip

    包含翻译后的API文档:akka-protobuf_2.11-2.4.20-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:com.typesafe.akka:akka-protobuf_2.11:2.4.20; 标签:typesafe、akka、protobuf、中英对照文档、jar包...

    DataSerialiser:Texxel使用的序列化库

    特征DataSerialiser具有以下功能: 简单的API 神奇的循环参考解析可读的序列化表格可能有多种不同的输出形式(仅当前正在编程的Json输出,但易于添加其他形式) 对象使用构造函数对其自身进行反序列化(使最终字段...

    mtproto:Telegram API的全本地go实现

    从发送请求到加密序列化的所有代码均使用纯golang编写。 您不需要获取任何其他依赖项。 最新的API版本(117+) Lib支持所有API和MTProto功能,包括视频通话和评论。 您可以创建其他请求请求来推送api更新! React性...

    使用Java实现一个基于内存的英文全文检索搜索引擎【100012394】

    内存里建立好的索引对象可以序列化到文件,同时可以从文件里反序列化成内存里的索引对象; 可以在控制台输出索引的内容。 功能 2:基于构建好的索引,实现单个搜索关键词的全文检索,包含的子功能包括: 根据搜索...

    english-hindi:带有Rails后端API的Ember前端,用于练习英语-印地语抽认卡和练习

    一个简单的抽认卡程序,可以帮助您记住印地语&lt;-&gt;英语单词翻译对。 要在本地运行,请确保从根目录执行以下操作: cd frontend npm install && bower ... 添加了ActiveModel序列化器 添加了300多个英语-印度语单

    SD大会精品讲座:什么让Web2.0 Mash-up如此强大(英语授课)

    这些Web服务不仅返回XML信息,有些还支持RSS、JSON和序列化PHP格式。在本课程中,Yahoo!的Web技术领域专家Dan Theurer将阐述今天驱动Mash-up的各种形式、资源和技术。Dan Theurer将以Yahoo!的各种Web服务和AJAX工具...

    python入门到高级全栈工程师培训 第3期 附课件代码

    Django序列化共6课 第61章 01 上节内容回顾 02 上传文件 03 制作上传按钮 04 Form组件上传文件 05 上传相关内容梳理 06 Model操作知识提问 07 Model操作概述 08 Model字段 09 Model连表字段参数详解 10 Model...

    Autocomplete-System:带有Neo4j图形后端的静态自动完成服务。 返回最佳建议

    服务器的序列化和反序列化,以进行数据交换。 如何使用 Python版本&gt; = 3.7 安装Python3 venv macOS/Linux sudo apt-get install python3-venv # If needed python3 -m venv env source env/bin/activate Windows ...

    flutter_realworld_app:用Flutter构建的示例现实世界应用程序

    怎么运行的使用来管理状态使用序列化/反序列化模型对i18n使用支持英语和中文语言环境!无限ListView受。干得好,伙计!入门要自己构建此应用,请执行以下操作:克隆此仓库并使用Android Studio打开它(别忘了安装...

    csvtools:在Nim中管理CSV文件

    尽管如此,自动键入反序列化已经非常方便了。 存在用于将类型化的序列写入CSV文件的对称API。 从T类型的对象到字符串序列的转换-或反之,从字符串序列到T -由宏处理。 该宏假定T为平面类型,这意味着其成员可以是...

    Java面试宝典2010版

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 75、描述一下JVM加载class文件的原理机制? 76、heap和stack有什么区别。 77、GC是什么? 为什么要有GC? 78、垃圾回收的优点和...

    Microsoft SQL Server 2005 Express Edition SP3

    Windows 加密服务提供程序 (CSP) 是执行身份验证、编码和加密服务的代码,基于 Windows 的应用程序通过 Windows Server 2003 中的 CryptoAPI 访问这些服务。如果停止或禁用 CSP 服务,则 SQL Server 安装程序会失败...

    Thinking in Java(中文版 由yyc,spirit整理).chm

    12.2.8 通过序列化进行深层复制 12.2.9 使克隆具有更大的深度 12.2.10 为什么有这个奇怪的设计 12.3 克隆的控制 12.3.1 副本构建器 12.4 只读类 12.4.1 创建只读类 12.4.2 “一成不变”的弊端 12.4.3 不变字串 ...

    最新Java面试宝典pdf版

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 51 75、描述一下JVM加载class文件的原理机制? 52 76、heap和stack有什么区别。 52 77、GC是什么? 为什么要有GC? 52 78、垃圾回收的...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    除在保密期内的保密论文和在技术保护期限内的论文外,允许论文被查阅和借阅,可以公布(包括以电子信息形式刊登)论文的全部内容或中、英文摘要等部分内容。论文的公布(包括以电子信息形式刊登)授权东南大学研究生...

    Java面试笔试资料大全

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 51 75、描述一下JVM加载class文件的原理机制? 52 76、heap和stack有什么区别。 52 77、GC是什么? 为什么要有GC? 52 78、垃圾回收的...

    Java面试宝典-经典

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 51 75、描述一下JVM加载class文件的原理机制? 52 76、heap和stack有什么区别。 52 77、GC是什么? 为什么要有GC? 52 78、垃圾回收的...

    JAVA面试宝典2010

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 51 75、描述一下JVM加载class文件的原理机制? 52 76、heap和stack有什么区别。 52 77、GC是什么? 为什么要有GC? 52 78、垃圾回收的...

    java面试题大全(2012版)

    74、什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。 51 75、描述一下JVM加载class文件的原理机制? 52 76、heap和stack有什么区别。 52 77、GC是什么? 为什么要有GC? 52 78、垃圾回收的...

Global site tag (gtag.js) - Google Analytics