java.io.Serializable序列化

本文深入探讨了Java序列化的原理及应用场景,介绍了实现Serializable接口的方法及其注意事项,并解释了序列化过程中的关键步骤。

     特别声明:

     *由于技术水平,英文水平有限,可能出现不当或错误,希望自己以后回头来看,不会笑掉大牙。

     *本文是原创,转载请注明原地址。

     *本文为学习笔记,请大家指正。

 

     第一章  序列化

            第1节 java.io.Serializable 接口

             java语音的一个承若,对象的可移动性,允许对象在网络上传输,实现对象的可移动性,使用的是序列化技术。(PS. 记录这些概念的天赋,正如我的语言天赋,半桶水。所以不多说)

             对象的序列化使用情形:

             1. 将对象的状态保存(保存到数据库、文件等)

              2. 网络通信传输对象(RMI,SOCKET)

              有序列化自然有反序列化,当我们需要读取序列化的对象时,我们需要用到反序列化。

              如何使用?

              让我们需要序列化的类实现java.io.Serializable 接口即可,通过如下方法调用:

              java.io.ObjectOutputStream.writeObject(Object)来序列化对象  java.io.ObjectInputStream.readObject()来反序列化对象

             关于具体如何使用将在下一节讨论。

             注意:*******

             我们实现Serlizable接口,即可实现序列化,但是有几点需要特别注意:

             1、 实现接口后,我们需要定义static final long serialVersionUID (虽然在序列化的时候,如果没有定义,java会自动帮助我们生成,但是根据对象的状态的不一样,编译环境等,会有可能产生InvalidClassException。并且强烈建议定义成private,应为子类中改属性毫无意义。)

             2、如果父类没有实现序列化接口,子类序列化无法序列化父类的属性,并且父类必须提供无参数的public protect构造方法(否则会抛出异常,反序列化时,会调用无参数构造方法,来初始化父类属性),所以父类的属性的序列化必须由子类来提供(可选择覆盖)。

             3、序列化和反序列化的类信息必须一样,包括serialVersionUID。

             4、 序列化时readObject方法只会读取non-static 和non-transient的属性。所以当不需要序列化时,可以定义属性为transient和static。

             5、可以实现Externalizable接口来定制自己的序列化和反序列化内容。

            完毕!

            上源码!

/*
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.io;

/**
 * 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
 * @see java.io.ObjectOutputStream
 * @see java.io.ObjectInputStream
 * @see java.io.ObjectOutput
 * @see java.io.ObjectInput
 * @see java.io.Externalizable
 * @since   JDK1.1
 */
public interface Serializable {
}


 

 

             

 

### Java `Serializable` 接口的作用 `Serializable` 是一个标记接口,用于指示某个类的对象可以通过标准机制进行序列化操作[^1]。当一个类实现了 `java.io.Serializable` 接口时,意味着它的实例可以被转换成字节流形式以便于在网络上传输或存储到文件中。 #### 主要作用 - **对象持久化**:允许将对象的状态保存至磁盘或其他外部介质上,并能够在稍后的时刻恢复其状态[^3]。 - **跨进程通信**:支持分布式环境中不同 JVM 之间传递复杂数据结构(如 RMI 或者消息队列中的消息体),前提是这些数据能够被序列化并反序列化回来[^4]。 --- ### 使用场景分析 以下是几个典型的使用案例: 1. **远程过程调用 (RPC)** 当应用程序需要通过网络发送对象作为参数或者返回值时,通常要求这些对象可序列化。例如,在微服务架构下客户端和服务端交互过程中经常涉及此类需求。 2. **缓存系统** 如果希望把某些临时性的计算结果暂存在内存以外的地方,则可能需要用到序列化技术来完成这一目标。 3. **数据库存储** 对象可以直接写入二进制格式的数据表字段里再读取出来重建原样副本[^5]. --- ### 如何实现 Serializable 接口? 由于它只是一个标志性质的空接口,因此只需要简单声明即可: ```java import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; // 可选但推荐设置 String name; transient int age; // 不会被序列化的成员变量前加上transient关键字修饰 public Person(String n, int a){ this.name=n; this.age=a; } } ``` 上述代码片段展示了如何创建一个基本的支持序列化的类。注意这里还包含了两个重要概念: - `serialVersionUID`: 这是用来验证版本一致性的唯一标识符。如果不显式指定,默认情况下编译器会基于类的内容生成一个哈希值。但是这样可能导致即使只是修改注释也会改变ID从而引发兼容性问题所以最好手动设定固定数值。 - `transient` 关键字标注那些不想参与序列化进程里的属性。 另外还可以重载默认行为来自定义更复杂的逻辑处理方式比如覆盖特殊的方法像 `writeObject()` 和 `readObject()`, 它们会在执行相应动作之前自动调用[^2]: ```java private void writeObject(java.io.ObjectOutputStream out) throws IOException{ out.defaultWriteObject(); } private void readObject(java.io.ObjectInputStream in )throws IOException , ClassNotFoundException{ in .defaultReadObject(); } ``` 以上例子演示了怎样扩展基础功能以满足特定业务的需求。 --- ### 注意事项 尽管方便易用,但在实际开发工作中应谨慎考虑是否真的有必要让某类型具备此特性因为这可能会带来额外负担包括但不限于性能损耗、维护成本上升等问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值