serialversionuid

Java中对象序列化与反序列化

…衆ロ難τιáo~ 提交于 2020-02-29 08:43:10
1. 概念 把对象转换为字节序列的过程称为对象的序列化。 把字节序列恢复为对象的过程称为对象的反序列化。 对象的序列化主要有两种用途: 把对象的字节序列永久地保存到 硬盘 上,通常存放在一个文件中; 在网络上传送对象的字节序列。   在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘,以便长期保存。比如最常见的是Web服务器中的Session对象,当有10万用户并发访问,就有可能出现10万个Session对象,内存可能吃不消,于是Web容器就会把一些seesion先序列化到硬盘中,等要用了,再把保存在硬盘中的对象还原到内存中。   当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象。 2. JDK类库中的序列化API java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。   java.io.ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。  

Java: Should serializable inner & anonymous classes have SerialVersionUID?

给你一囗甜甜゛ 提交于 2020-01-21 05:20:12
问题 Although I'm not currently planning to serialize anything, I give all serializable outer classes, as well as static nested classes a SerialVersionUID , because that is the proper way to do it. However, I've read here that Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. ... So my question is: Should I give inner and anonymous classes a SerialVersionUID each, or should I

Java: Should serializable inner & anonymous classes have SerialVersionUID?

这一生的挚爱 提交于 2020-01-21 05:20:08
问题 Although I'm not currently planning to serialize anything, I give all serializable outer classes, as well as static nested classes a SerialVersionUID , because that is the proper way to do it. However, I've read here that Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. ... So my question is: Should I give inner and anonymous classes a SerialVersionUID each, or should I

Can NetBeans generate an automatic serial version ID for a Java class?

社会主义新天地 提交于 2020-01-14 08:14:10
问题 I would like to remove some warnings for some classes by generating an automatic serial version ID. In Eclipse, this is trivial to do - the IDE can generate one automatically and add it to the class. However, I don't see this functionality in NetBeans. Is it available? If so, where is it? If not, is there a plugin that can enable it? 回答1: Actually my solution to that "problem" was to deactivate that warning in my project configuration (I use Eclipse but I guess for NetBeans is the same) as

How to prevent the auto-generation of comments just for quick-fixing the serialVersionUID warning?

风格不统一 提交于 2019-12-25 03:22:28
问题 This Eclipse question is a response to "Getting rid of the comment above eclipse generated serialVersionUID," but so far the single answer (which addresses this linked question, and is accepted) applies to all comments. But my question will be more specific (it is not a duplicate) - I want normal generation of comments for other fields, just no generation of the comment when you quick-fix the serializable warning by generating the field. As what the accepted answer to the original question,

How is serialVersionUID serialized in Java?

戏子无情 提交于 2019-12-24 03:06:35
问题 Class members (static) cannot be serialized. The reason is obvious - they are not held by the object(s) of the class. Since they are associated with the class (rather than the object of that class), they are stored separately from the object. serialVersionUID is declared as a static field within a class that implements the java.io.Serializable interface something like the following. private static final long serialVersionUID = 1L; It is used as a version control in a Serializable class. If it

What algorithm is used by eclipse to generate verison id in Serializable class?

。_饼干妹妹 提交于 2019-12-23 10:34:08
问题 Suppose here is my class : class B implements Serializable { private static final long serialVersionUID = -5186261241138469827L; // what algo is used to generate this .......... } What algorithm eclipse uses to generate serialVersionUID = -5186261241138469827L ? 回答1: Eclipse implements the relevant Java spec to compute Serialization ID. In Eclipse, this is implemented by calculateSerialVersionId method in org.eclipse.jdt.internal.ui.text.correction.SerialVersionHashOperation class. 回答2: Java

How to get rid of InvalidClassException SerialVersionUID?

泪湿孤枕 提交于 2019-12-22 04:28:20
问题 I had saved one java object in the Database and then after few days I changed my jre version. Now when i tried to read that same object I am getting following exception: Exception in thread "main" java.io.InvalidClassException: SerializeMe; local class incompatible: stream classdesc serialVersionUID = -6377573678240024862, local class serialVersionUID = -8204757486033751616 How can I get rid of this,how can I get the saved object? please help me. 回答1: If you can affect source code of this

Where do I change the setting in Eclipse so that it would generate serialVersionUID for Serializable classes?

非 Y 不嫁゛ 提交于 2019-12-20 03:17:29
问题 I want Eclipse to generate a serialVersionUID for classes that implement Serializable . However, my Eclipse instance does not throw me a warning or an error when I create a class that implements Serializable . Also, it does not give an suggestion about adding a generated serialVersionUID . Where do I change the required setting? 回答1: To turn on warning for Serializable class without a serialVersionID, go to Window > Preference > Java > Compiler > Errors/Warnings and search for Serializable

Why my exception class needs to be serialized?

江枫思渺然 提交于 2019-12-18 10:44:45
问题 When you extend a class with class Exception ( for creating new exception) you get a warning to have a serialVersionUID . I know that serialVersionUID plays an important role while serialization and deserialization, but when my Exception needs to be serialized? Can anyone give me a practical case in which I want my custom-exception class to have serialization and deserialization? 回答1: This is because the root class for all exceptions, Throwable implements the Serializable interface. All