Java对象序列化格式二

假设有以下类:

package test;

import java.io.Serializable;

public class Bean implements Serializable{
	private static final long serialVersionUID = -1124040661644314220L;
	private int a;
	private int b;

	private String name;

	private int[] ages;

	public String getName() {
		return name;
	}

	public void setAges(int[] ages) {
		this.ages = ages;
	}

	public int[] getAges() {
		return ages;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getA() {
		return a;
	}

	public int getB() {
		return b;
	}

	public void setA(int a) {
		this.a = a;
	}

	public void setB(int b) {
		this.b = b;
	}
}

 

 

 

构造一个这个类的对象,并设置它的变量值:

		Bean bean = new Bean();
		bean.setA(4);
		bean.setB(5);
		bean.setAges(new int[]{23,44,55});
		bean.setName("Liugang");

 

最后序列化保存的结果如下:

序列化结果

 

下面是对其中每个字节的说明:

下面我将详细分析这个结果。在分析之前需要知道Java中的序列化格式规范,这个可以参考:stream.doc

	AC 	ED	magic
	00   	05	version
	73		TC_OBJECT
		72	TC_CLASSDESC
		00	09	length of className
		74 65 73 74 2E 42 65 61 6E	test.Bean
		F0 66 9A F9 41 98 79 94	serialVersionUID
		02	implement Serializable
		00	04	Number of Fields
		49	I, int type
		00	01	length of field name
		61	a, field name
		49	I, int type
		00	01	length of field name
		62	b, field name
		5B	[, array type
		00	04	length of field name
		61 67 65 73	ages, field name
		74 	TC_STRING, string type
	00 02	length of field type
	5B 49	[I, type name
		4C	L, object type
		00	04	length of field name
		6E 61 6D 65	field name
	74	TC_STRING, string type
	00 12	18, length of field type
	4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B
	field type, Ljava/lang/String;
		78		TC_ENDBLOCKDATA
		70		TC_NULL, No Super
		00 00 00 04	first field value, here is a.
		00 00 00 05	second value, here is b.
		75		TC_ARRAY, means array
			72	TC_CLASSDESC
			00 02	length of type, [I
			5B 49	[I
			4D BA 60 26 76 EA B2 A5	serial name of [I
			02	implement Serializable
			00 00	number of fields
			78	TC_ENDBLOCKDATA
			70	TC_NULL, no super
			00 00 00 03	Length of array
			00 00 00 17	first number, 23
			00 00 00 2C	second number, 44
			00 00 00 37	third number, 55
		74		TC_STRING, string type
			00 07	length of value
			4C 69 75 67 61 6E 67	value, “Liugang”
	

注:
1.	78是一个终止符
2.	要想查看某个对象类型的序列号,可以到jdk/bin目录下运行:
       serialver -show
3.	对于有父类的对象,父类需要也是一个可序列化的对象,即实现接口Serializable或Externalizble。然后以父类对象做同样的步骤。

  

 

你可能感兴趣的:(java,jdk,bean)