Serializable是接口吗?public class DeleteBean implements Serializable这个类去实现它有啥作用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Serializable是接口吗?public class DeleteBean implements Serializable这个类去实现它有啥作用?相关的知识,希望对你有一定的参考价值。

Serializable是序列化接口。
而且是一个标志接口,就是接口里什么也没有,空的。

序列化的意思就是将一个对象在内存里的形态记录,让它可以存在文件中,也可以方便的在网络中传输

但是序列化的代价是非常高的,所以需要有一个身份登记一样的东西,就是Serializable接口,系统对一个对象序列化的时候先判断你是否实现了Serializable接口,实现了的才可以序列化,否则要报错
参考技术A Serializable是序列化接口。
简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来。虽然你可以用你自己的各种各样的方法来保存object states,但是Java给你提供一种应该比你自己好的保存对象状态的机制,那就是序列化。

参考资料:http://www.javaeye.com/topic/121311

参考技术B 这个是Java中的一个接口存在于java.io中,是一个实现可序列话的接口。
如果你学过ejb那么你就知道你的bean都需要实现这个可序列话的接口。
我们的建议就是即使你不适用ejb。而是使用Spring也最好实现一下这个可序列话的接口,便与以后将应用转向ejb。实现这个接口的作用就是便与数据传输。

关于java Serializable接口的问题

那个实现序列化
private static final long serialVersionUID = 3674727123335529803L;
中3674727123335529803L是怎么定义的?随便可以吗?
-_-!!这么麻烦。 我想写个聊天工具,看到这里觉得头晕了,该这么办?

是返回一个序列化对象,返回序列化对象的目的是为了用instanceof进行比较。
上面的意思就是说o是object对象,当调用add方法的时候o就实现了序列化,是一个序列化的对象,就可以调用instanceof进行判断
再说说为什么要实现序列化:
其实很简单,我们平时说的int,double等等类型的数据之所以能保存到电脑上,而且还可以再读出来,就是因为他们的包装类interger,double等实现了序列化。所以我们就可以用输入输出流进行操作,而且属性不会变。如果我们想把一个对象进行这样的操作,那么我们就必须让这个对象实现序列化。
不知道你明白了没有。。。其实网上好多这方面的解释,多看看肯定会明白的。。。
参考技术A 是工具自己生成的,你可以不去管他。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.
参考技术B 这个可以不用理睬它。
它实际的作用时防止一个类变动后,在反序列化老版本的对象实例时出错。
不过通常应用都用不到这个功能的。

通常写serialVersionUID = 1L也可以。
甚至,不写这行也仅会导致编译器报警而已。

另外,一个系统中两个类有同一个serialVersionUID不会有任何问题。仅仅需要在同一个类的代码改动后,为区分版本而需要改个不同的值。本回答被提问者和网友采纳
参考技术C 这个是可以通过工具生成的。
不能随便定义。
如果一个系统中两个类的serialVersionUID相同,
会发生不可预知的情况。

以上是关于Serializable是接口吗?public class DeleteBean implements Serializable这个类去实现它有啥作用?的主要内容,如果未能解决你的问题,请参考以下文章

Java实体对象为什么要实现Serializable接口?

Serializable中的serialVersionUID到底有啥用

(小白)用java保存(读取)对象到本地 对象中所有的调用的类都要实现Serializable接口吗

关于java Serializable接口的问题

关于JavaBean实现Serializable接口的见解

为什么要实现Serializable