星期八职场经验网()【现成经验助你快速完成工作】本文将对Java字节码进行解析,以及一些效率问题。AD:一:Java字节代码的组织形式类文件{OxCAFEBABE,小版本号,大版本号,常量池大小,常量池数组,访问控制标记,当前类信息,父类信息,实现的接口个数,实现的接口信息数组,域个数,域信息数组,方法个数,方法信息数组,属性个数,属性信息数组}二:查看方法---javap命令例子:有一个Java类Demo.java1.publicclassDemo{2.privateStringstr1;3.privateStringstr2;4.privateintnum1;5.privateintnum2;6.publicstaticfinalStringSTATIC_DATA=helloworld;7.8.privatevoidsayHello1(){9.System.out.println(thisismethod1...);10.}11.privatevoidsayHello2(){12.System.out.println(thisismethod2...);13.}14.publicvoidsayHello3(){15.System.out.println(thisismethod3...);16.}17.}通过jdk自带的反编译工具命令javap可以查看class文件的字节码信息星期八职场经验网()【现成经验助你快速完成工作】D:\javap-verboseDemoDemo.txtDemo.txt:1.CompiledfromDemo.java2.publicclassDemoextendsjava.lang.Object3.SourceFile:Demo.java4.minorversion:05.majorversion:496.7.Constantpool:8.const#1=class#2;//Demo9.const#2=AscizDemo;10.const#3=class#4;//java/lang/Object11.const#4=Ascizjava/lang/Object;12.const#5=Ascizstr1;13.const#6=AscizLjava/lang/String;;14.const#7=Ascizstr2;15.const#8=Asciznum1;16.const#9=AscizI;17.const#10=Asciznum2;18.const#11=AscizSTATIC_DATA;19.const#12=AscizConstantValue;20.const#13=String#14;//helloworld21.const#14=Ascizhelloworld;22.const#15=Ascizinit;23.const#16=Asciz()V;24.const#17=AscizCode;25.const#18=Method#3.#19;//java/lang/Object.init:()V26.const#19=NameAndType#15:#16;//init:()V27.const#20=AscizLineNumberTable;28.const#21=AscizLocalVariableTable;29.const#22=Ascizthis;30.const#23=AscizLDemo;;31.const#24=AscizsayHello1;32.const#25=Field#26.#28;//java/lang/System.out:Ljava/io/PrintStream;星期八职场经验网()【现成经验助你快速完成工作】33.const#26=class#27;//java/lang/System34.const#27=Ascizjava/lang/System;35.const#28=NameAndType#29:#30;//out:Ljava/io/PrintStream;36.const#29=Ascizout;37.const#30=AscizLjava/io/PrintStream;;38.const#31=String#32;//thisismethod1...39.const#32=Ascizthisismethod1...;40.const#33=Method#34.#36;//java/io/PrintStream.println:(Ljava/lang/String;)V41.const#34=class#35;//java/io/PrintStream42.const#35=Ascizjava/io/PrintStream;43.const#36=NameAndType#37:#38;//println:(Ljava/lang/String;)V44.const#37=Ascizprintln;45.const#38=Asciz(Ljava/lang/String;)V;46.const#39=AscizsayHello2;47.const#40=String#41;//thisismethod2...48.const#41=Ascizthisismethod2...;49.const#42=AscizsayHello3;50.const#43=String#44;//thisismethod3...51.const#44=Ascizthisismethod3...;52.const#45=AscizSourceFile;53.const#46=AscizDemo.java;54.55.{56.publicstaticfinaljava.lang.StringSTATIC_DATA;57.Constantvalue:Stringhelloworld58.publicDemo();59.Code:60.Stack=1,Locals=1,Args_size=161.0:aload_062.1:invokespecial#18;//Methodjava/lang/Object.init:()V63.4:return64.LineNumberTable:65.line2:066.LocalVariableTable:67.StartLengthSlotNameSignature68.050thisLDemo;星期八职场经验网()【现成经验助你快速完成工作】69.70.publicvoidsayHello3();71.Code:72.Stack=2,Locals=1,Args_size=173.0:getstatic#25;//Fieldjava/lang/System.out:Ljava/io/PrintStream;74.3:ldc#43;//Stringthisismethod3...75.5:invokevirtual#33;//Methodjava/io/PrintStream.println:(Ljava/lang/String;)V76.8:return77.LineNumberTable:78.line17:079.line18:880.LocalVariableTable:81.StartLengthSlotNameSignature82.090thisLDemo;83.}解析:1.版本号majorversion:49//java版本jdk1.6显示的是50,jdk1.5显示的是49,jdk1.4显示的是58,高版本能执行低版本的class文件2.常量池ConstantpoolMethod:方法Field:字段String:字符串Asciz:签名如init由jvm调用,其他是不能够去调用它的NameAndType:变量名的类型星期八职场经验网()【现成经验助你快速完成工作】Class:类通过字节码,我们可以看到Demo类继承于java.lang.Object,如果类中没有显式声明构造函数的话,编译器会插入一个缺省无参的构造函数(构造函数在JVM级别是显示成init的普通函数)。三:检测代码的效率问题学习Java的过程中,都会了解到字符串合并时要用到StringBuffer来代替String,那下面就来通过Java字节码来验证两种方式的效率性。例子:一个Java类TestString.java1.strongpublicclassTestString{2.publicStringtestString(Stringstr1,Stringstr2){3.returnstr1+str2;4.}5.publicStringtestStringBuffer(StringBuffersb,Stringstr){6.returnsb.append(str).toString();7.}8.}9./strongjavap–cTestString后字节码信息:1.CompiledfromTestString.java2.publicclassTestStringextendsjava.lang.Object{3.publicTestString();4.Code:5.0:aload_06.1:invokespecial#8;//Methodjava/lang/Object.init:()V7.4:return8.9.publicjava.lang.StringtestString(java.lang.String,java.lang.String);10.Code:11.0:new#16;//classjava/lang/StringBuilder12.3:dup星期八职场经验网()【现成经验助你快速完成工作】13.4:aload_114.5:invokestatic#18;//Methodjava/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;15.8:invokespecial#24;//Methodjava/lang/StringBuilder.init:(Ljava/lang/String;)V16.11:aload_217.12:invokevirtual#27;//Methodjava/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;18.15:invokevirtual#31;//Methodjava/lang/StringBuilder.toString:()Ljava/lang/String;19.18:areturn20.21.publicjava.lang.StringtestStringBuffer(java.lang.StringBuffer,java.lang.String);22.Code:23.0:aload_124.1:aload_225.2:invokevirtual#40;//Methodjava/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;26.5:invokevirtual#45;//Methodjava/lang/StringBuffer.toString:()Ljava/lang/String;27.8:areturn28.}从上面编译后的字节码信息可以看出来,方法testString调用了五个方法:new、invokestatic、i