In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java bytecode case analysis", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java bytecode case analysis"!
One: the organization of Java bytecode
Class file {
OxCAFEBABE, minor version, large version, constant pool size, constant pool array, access control tag, current class information, parent information, number of interfaces implemented, implemented interface information array, number of fields, domain information array, method information array, attribute information array, attribute information array
}
Two: view method-javap command
Example: there is a Java class Demo.java
Public class Demo {private String str1; private String str2; private int num1; private int num2; public static final String STATIC_DATA = "hello world"; private void sayHello1 () {System.out.println ("this is method1...");} private void sayHello2 () {System.out.println ("this is method2...") } public void sayHello3 () {System.out.println ("this is method3...");}}
The bytecode information of the class file can be viewed through the decompiler command javap that comes with jdk.
D:\ > javap-verbose Demo > > Demo.txt
Demo.txt:
Compiled from "Demo.java" public class Demo extends java.lang.Object SourceFile: "Demo.java" minor version: 0 major version: 49 Constant pool: const # 1 = class # 2; / / Demo const # 2 = Asciz Demo; const # 3 = class # 4; / / java/lang/Object const # 4 = Asciz java/lang/Object; const # 5 = Asciz str1; const # 6 = Asciz Ljava/lang/String Const # 7 = Asciz str2; const # 8 = Asciz num1; const # 9 = Asciz I; const # 10 = Asciz num2; const # 11 = Asciz STATIC_DATA; const # 12 = Asciz ConstantValue; const # 13 = String # 14; / / hello world const # 14 = Asciz hello world; const # 15 = Asciz; const # 16 = Asciz () V; const # 17 = Asciz Code; const # 18 = Method # 3.019 / / java/lang/Object. "": () V const # 19 = NameAndType # 15 const 16 characters / "": () V const # 20 = Asciz LineNumberTable; const # 21 = Asciz this; const # 22 = Asciz LDemo;; const # 23 = Asciz LDemo;; const # 24 = Asciz sayHello1; const # 25 = Field # 26.328; / / java/lang/System.out:Ljava/io/PrintStream; const # 26 = class # 27 / / java/lang/System const # 27 = Asciz java/lang/System; const # 28 = NameAndType # 29 out:Ljava/io/PrintStream; const # 29 = Asciz out; const # 30 = Asciz Ljava/io/PrintStream;; const # 31 = String # 32; / / this is method1... Const # 32 = Asciz this is method1...; const # 33 = Method # 34.33; / / java/io/PrintStream.println: (Ljava/lang/String;) V const # 34 = class # 35; / / java/io/PrintStream const # 35 = Asciz java/io/PrintStream; const # 36 = NameAndType # 37 println: (Ljava/lang/String;) V const # 37 = Asciz println; const # 38 = Asciz (Ljava/lang/String;) V Const # 39 = Asciz sayHello2; const # 40 = String # 41; / / this is method2... Const # 41 = Asciz this is method2...; const # 42 = Asciz sayHello3; const # 43 = String # 44; / / this is method3... Const # 44 = Asciz this is method3...; const # 45 = Asciz SourceFile; const # 46 = Asciz Demo.java; {public static final java.lang.String STATIC_DATA; Constant value: String hello world public Demo (); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial # 18 / / Method java/lang/Object. "": () V 4: return LineNumberTable: line 2: 0 LocalVariableTable: Start Length Slot Name Signature 050 this LDemo; public void sayHello3 (); Code: Stack=2, Locals=1, Args_size=1 0: getstatic # 25; / / Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc # 43; / / String this is method3... 5: invokevirtual # 33; / / Method java/io/PrintStream.println: (Ljava/lang/String;) V 8: return LineNumberTable: line 17: 0 line 18: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 this LDemo;}
Parsing:
1. Version number: major version: 49 / / java version jdk1.6 shows 50, jdk1.5 shows 49 jdk1.4 shows 58, high version can execute lower version of class file
two。 Constant pool Constant pool
Method: method
Field: field
String: string
Asciz: if the signature is called by jvm, others cannot call it.
NameAndType: type of variable name
Class: class
Through bytecode, we can see that the Demo class inherits from java.lang.Object, and if the constructor is not explicitly declared in the class, the compiler inserts a default no-argument constructor (the constructor is a normal function displayed at the JVM level).
Third, the efficiency of testing code
In the process of learning Java, you will learn that StringBuffer is used instead of String in string merging, so let's verify the efficiency of the two ways by Java bytecode.
Example: a Java class TestString.java
Public class TestString {public String testString (String str1, String str2) {return str1 + str2;} public String testStringBuffer (StringBuffer sb, String str) {return sb.append (str). ToString ();}}
Javap-c bytecode information after TestString:
Compiled from "TestString.java" public class TestString extends java.lang.Object {public TestString (); Code: 0: aload_0 1: invokespecial # 8; / / Method java/lang/Object. "": () V 4: return public java.lang.String testString (java.lang.String, java.lang.String); Code: 0: new # 16 / / class java/lang/StringBuilder 3: dup 4: aload_1 5: invokestatic # 18; / / Method java/lang/String.valueOf: (Ljava/lang/Object;) Ljava/lang/String; 8: invokespecial # 24; / / Method java/lang/StringBuilder. ": (Ljava/lang/String;) V 11: aload_2 12: invokevirtual # 27 / / Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder; 15: invokevirtual # 31; / / Method java/lang/StringBuilder.toString: () Ljava/lang/String; 18: areturn public java.lang.String testStringBuffer (java.lang.StringBuffer, java.lang.String); Code: 0: aload_1 1: aload_2 2: invokevirtual # 40 / / Method java/lang/StringBuffer.append: (Ljava/lang/String;) Ljava/lang/StringBuffer; 5: invokevirtual # 45; / / Method java/lang/StringBuffer.toString: () Ljava/lang/String; 8: areturn}
As you can see from the compiled bytecode information above, the method testString calls five methods: new, invokestatic, invokespecial, and two invokevirtual, while the testStringBuffer method calls only two invokevirtual methods. The * method does a lot more work than the second method, and its efficiency is of course inefficient. And we Ljava/lang/StringBuilder from java/lang/StringBuilder.append: (Ljava/lang/String;)
You can see that in fact, for String string merging, the internal is still converted to StringBuilder method calls, this is because the length of String is immutable, so it is better to directly use StringBuilder (and StringBuffer length are variable, but the former is non-thread-safe, the latter is thread-safe) for string merging.
Thank you for your reading, the above is the content of "Java bytecode example analysis", after the study of this article, I believe you have a deeper understanding of the problem of Java bytecode example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.