0
我是序列化的新手。序列化奇怪的結果
我試着編寫簡單的代碼。 我得到了奇怪的結果。我不明白。
我有這樣的代碼:
package easybudda.fakesizeof;
import java.io.*;
public class FakeSizeOf {
public static void main(String[] args) throws IOException {
Object[] arr = { new Boolean(true), new Character('ы'), new Integer(13), new Long(69L), new Double(3.14) };
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
for (Object obj : arr) {
outStream.reset();
objOutStream.writeObject(obj);
objOutStream.flush();
System.out.println(obj.getClass().getSimpleName() + ": " + outStream.size());
}
System.out.println("\nUsing predefined methods:");
outStream.reset();
objOutStream.write(13);
objOutStream.flush();
System.out.println("write with int as data: " + outStream.size());
outStream.reset();
objOutStream.writeBoolean(false);
objOutStream.flush();
System.out.println("writeBoolean: " + outStream.size());
outStream.reset();
objOutStream.writeByte(1);
objOutStream.flush();
System.out.println("writeByte: " + outStream.size());
outStream.reset();
objOutStream.writeChar('ы');
objOutStream.flush();
System.out.println("writeChar: " + outStream.size());
outStream.reset();
objOutStream.writeDouble(3.14);
objOutStream.flush();
System.out.println("writeDouble: " + outStream.size());
outStream.reset();
objOutStream.writeInt(13);
objOutStream.flush();
System.out.println("writeInt: " + outStream.size());
outStream.reset();
objOutStream.writeLong(69L);
objOutStream.flush();
System.out.println("writeLong: " + outStream.size());
}
}
輸出:
Boolean: 43
Character: 46
Integer: 77
Long: 51
Double: 53
Using predefined methods:
write with int as data: 3
writeBoolean: 3
writeByte: 3
writeChar: 4
writeDouble: 10
writeInt: 6
writeLong: 10
可有人exlpain這些結果給我嗎?
1.for例如int - 4字節(6輸入) - 爲什麼?
2.爲什麼從數組中獲得如此巨大的值?
1.如何序列INT原始? 2.爲什麼整數從數組 - 77? – gstackoverflow