简体   繁体   English

Java Serializable

[英]Java Serializable

i have made a serialized class,like this 我已经制作了一个序列化的类,就像这样

` `

class Example implements Serializable
{
    transient byte i=2;
    transient byte j=3;
    Example(){System.out.println("value of i:"+i+",j:"+j);}
}

` `
when i am serilizing n deserializing the class,ie 当我对这个类进行反序列化时,即

    class SerialClass
{
public static void main(String []r)//throws IOException
{
try{
    System.out.println("Serialization");
    Example e=new Example();
    FileOutputStream out=new FileOutputStream("hyxa_code.txt");
/*File f=new File("copt.txt");
f.createNewFile();*/
    ObjectOutputStream oo=new ObjectOutputStream(out);
    oo.writeObject(e);
    oo.close();}catch(IOException e){}


try{
System.out.println("Deserialization");
    Example ee=new Example();
FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);
}catch(IOException e)
{}
catch(ClassNotFoundException e){}
}
}

the output is coming like this: 输出是这样的:

Serialization
value of i:2,j:3
Deserialization
value of i:2,j:3
The vlaue of i,j:0 0

but as i have heard, Deserialization doesn't initialize constructor, here is happening,why??? 但正如我所听到的,反序列化不会初始化构造函数,这里正在发生,为什么??? also,why the value of both variables is coming as it was initialized 另外,为什么两个变量的值都在初始化时出现

Having the variables marked as transient keeps them from being serialized. 将变量标记为瞬态会使它们不被序列化。 Remove the transient part of the variable declaration and it will work. 删除变量声明的瞬态部分,它将起作用。

And you are correct about the constructor not being invoked as part of deserialization. 并且你对作为反序列化的一部分不被调用的构造函数是正确的。 The state of the object is loaded directly from the stream and no constructor is invoked. 对象的状态直接从流加载,不调用构造函数。

See http://www.rockstarprogrammer.org/post/2006/jul/29/more_you_want_know_about_java_serialization/ for more information. 有关更多信息,请参阅http://www.rockstarprogrammer.org/post/2006/jul/29/more_you_want_know_about_java_serialization/ The readObject method can be used to initialize transient variables when deserialization happens. 当反序列化发生时,readObject方法可用于初始化瞬态变量。

You're explicitly calling the constructor twice - once to serialize and then once when deserializing: 您显式调用构造函数两次 - 一次序列化,然后在反序列化时调用一次:

Example ee=new Example();
FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);

You're ignoring the value you've created though, so the code is effectively like this: 您忽略了您创建的值,因此代码实际上是这样的:

FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
Example ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);

The difference is just that this time you won't be calling the constructor unnecessarily. 不同之处在于,这次你不会不必要地调用构造函数。

That explains why you're seeing the "value of i:2,j:3" line twice. 这就解释了为什么你会两次看到“i:2,j:3”的值。 The reason that the deserialized objects have values of 0 is because you've declared the variables as transient , as described in the other answers. 反序列化对象的值为0的原因是因为您已将变量声明为transient变量,如其他答案中所述。

Transient variables can't be serialized hence the value of variables became 0. 瞬态变量无法序列化,因此变量值变为0。

Here you have an example : http://javatechnologyhelper.blogspot.com/2014/04/java-serialization.html 这里有一个例子: http//javatechnologyhelper.blogspot.com/2014/04/java-serialization.html

Also, you can take a look at: http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient . 另外,您可以查看: http//en.wikibooks.org/wiki/Java_Programming/Keywords/transient

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. transient是一个Java关键字,它标记成员变量在被保存为字节流时不被序列化。 When an object is transferred through the network, the object needs to be 'serialized'. 当通过网络传输对象时,该对象需要“序列化”。 Serialization converts the object state to serial bytes. 序列化将对象状态转换为串行字节。 Those bytes are sent over the network and the object is recreated from those bytes. 这些字节通过网络发送,并从这些字节重新创建对象。 Member variables marked by the java transient keyword are not transferred, they are lost intentionally. 由java transient关键字标记的成员变量不会被转移,它们会被故意丢失。

As both i and j are marked as transient they will not be serialized. 由于i和j都被标记为瞬态,因此它们不会被序列化。 So when you deserialize they will have the default value for byte, which is 0. 因此,当您反序列化时,它们将具有byte的默认值,即0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM