标签:des style blog class code java
欢迎转载,转载需声明出处
------------------
请先看上一篇:Class类文件结构浅析
上一篇讲的都是理论,下面我们亲自实践一下。
public class Test { private int m; private String str; public int func(int m,String str) { str += "OK"; m = 10; return -1; } public static void main(String[] arg) { String str = "test"; int m = 20; new Test().func(m,str); } }
C:\Users\Rowand jj\Desktop>javap -verbose Test Classfile /C:/Users/Rowand jj/Desktop/Test.class Last modified 2014-5-2; size 616 bytes MD5 checksum cfd6b2e7d9d99dda3b84d1dbe87ce657 Compiled from "Test.java" public class Test SourceFile: "Test.java" minor version: 0 major version: 51 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #11.#26 // java/lang/Object."<init>":()V #2 = Class #27 // java/lang/StringBuilder #3 = Methodref #2.#26 // java/lang/StringBuilder."<init>":( )V #4 = Methodref #2.#28 // java/lang/StringBuilder.append:(Lj ava/lang/String;)Ljava/lang/StringBuilder; #5 = String #29 // OK #6 = Methodref #2.#30 // java/lang/StringBuilder.toString:( )Ljava/lang/String; #7 = String #31 // test #8 = Class #32 // Test #9 = Methodref #8.#26 // Test."<init>":()V #10 = Methodref #8.#33 // Test.func:(ILjava/lang/String;)I #11 = Class #34 // java/lang/Object #12 = Utf8 m #13 = Utf8 I #14 = Utf8 str #15 = Utf8 Ljava/lang/String; #16 = Utf8 <init> #17 = Utf8 ()V #18 = Utf8 Code #19 = Utf8 LineNumberTable #20 = Utf8 func #21 = Utf8 (ILjava/lang/String;)I #22 = Utf8 main #23 = Utf8 ([Ljava/lang/String;)V #24 = Utf8 SourceFile #25 = Utf8 Test.java #26 = NameAndType #16:#17 // "<init>":()V #27 = Utf8 java/lang/StringBuilder #28 = NameAndType #35:#36 // append:(Ljava/lang/String;)Ljava/l ang/StringBuilder; #29 = Utf8 OK #30 = NameAndType #37:#38 // toString:()Ljava/lang/String; #31 = Utf8 test #32 = Utf8 Test #33 = NameAndType #20:#21 // func:(ILjava/lang/String;)I #34 = Utf8 java/lang/Object #35 = Utf8 append #36 = Utf8 (Ljava/lang/String;)Ljava/lang/StringBuilder; #37 = Utf8 toString #38 = Utf8 ()Ljava/lang/String; { public Test(); flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 public int func(int, java.lang.String); flags: ACC_PUBLIC Code: stack=2, locals=3, args_size=3 0: new #2 // class java/lang/StringBuilder 3: dup 4: invokespecial #3 // Method java/lang/StringBuilder. "<init>":()V 7: aload_2 8: invokevirtual #4 // Method java/lang/StringBuilder. append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 11: ldc #5 // String OK 13: invokevirtual #4 // Method java/lang/StringBuilder. append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 16: invokevirtual #6 // Method java/lang/StringBuilder. toString:()Ljava/lang/String; 19: astore_2 20: bipush 10 22: istore_1 23: iconst_m1 24: ireturn LineNumberTable: line 8: 0 line 9: 20 line 10: 23 public static void main(java.lang.String[]); flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=3, args_size=1 0: ldc #7 // String test 2: astore_1 3: bipush 20 5: istore_2 6: new #8 // class Test 9: dup 10: invokespecial #9 // Method "<init>":()V 13: iload_2 14: aload_1 15: invokevirtual #10 // Method func:(ILjava/lang/String ;)I 18: pop 19: return LineNumberTable: line 15: 0 line 16: 3 line 17: 6 line 18: 19 }大家可能看的有点晕,不要紧,我们一步一步来分析。下面我们使用winhex打开Test.class文件:
ClassFile { u4 magic;//魔数(0xCAFEBABE) u2 minor_version;//次版本号 u2 major_version;//主版本号 u2 constant_pool_count;//常量池容量计数值 cp_info constant_pool[constant_pool_count-1];//常量池 u2 access_flags;//访问标志 u2 this_class;//类索引 u2 super_class;//父类索引 u2 interfaces_count;//接口计数器 u2 interfaces[interfaces_count];//接口索引集合 u2 fields_count;//字段计数器 field_info fields[fields_count];//字段表 u2 methods_count;//方法计数器 method_info methods[methods_count];//方法表 u2 attributes_count;//属性表计数器 attribute_info attributes[attributes_count];//属性表集合 }
标签:des style blog class code java
原文地址:http://blog.csdn.net/chdjj/article/details/24872179