Use Java to negotiate editors to understand Java features

xiaoxiao2021-03-06  57

With javap disassemble help you understand java properties (using javap drill class file) Author: Builder.com use javap drill class file

Java developers are familiar with using StringBuffer in a cycle instead of the series String objects to achieve optimal performance. However, most developers have never compared the difference between the bytecodes generated by the two methods. There is a tool called Java in the Java Development Kit (JDK) to tell you why do you have best performance.

Javap outputs some dump information of a class and its method to a standard output. This tool does not compile the code to the Java source code, but it will connect the byte code to the byte code instruction defined by the Java virtual machine specification.

When you need to see the compiler to do anything for you or give you something, or if you want to see what the code has an impact on the compiled class file, Java is quite useful.

Now the StringBuffer and String mentioned earlier as an example. Below is a class specifically designed to be designed. It has two methods that return a string consisting of 0 to N, where n is provided by the caller. The only difference between the two methods is that a String build result is used, and another uses the StringBuffer build result.

Public class javaptip {public static void main (string [] args) {}

Private static string withstrings (int count) {string s = ""; for (int i = 0; i

Return S;

Private static string withstringbuffer (int count) {stringbuffer sb = new stringbuffer (); for (int i = 0; i

Return sb.tostring ();}}

Let us now look at the output of this class using the -c option. The -c option tells the Javap to disassemble the byte code encountered in the class.

The operation is as follows:

> javap -c javaptip

The output of this command is:

Method java.lang.string withstrings (int) 0 ldc # 2

2 ASTORE_1

3 iconst_0

4 iStore_2

5 goto 30

8 new # 3

11 Dup

12 InvokeSpecial # 4

15 ALOAD_1

16 Invokevirtual # 5

19 ILOAD_2

20 Invokevirtual # 6

23 Invokevirtual # 7

26 ASTORE_1

27 IINC 2 1

30 ILOAD_2

31 ILOAD_0

32 IF_ICMPLT 8

35 ALOAD_1

36 areeturn

Method java.lang.string withstringbuffer (int) 0 new # 3

3 dup

4 InvokeSpecial # 4

7 ASTORE_1

8 iconst_0

9 ISTORE_2

10 goto 22

13 ALOAD_1

14 ILOAD_2

15 Invokevirtual # 6

18 POP

19 IINC 2 1

22 ILOAD_2

23 ILOAD_0

24 if_icmplt 1327 aload_1

28 Invokevirtual # 7

31 ARETURN

If you haven't seen the Java assembler before, then this output will be more difficult to understand, but you should be able to see the WITHSTRING method newly created a StringBuffer instance when each cycle is cycled. Then it adds the current value of the existing String to StringBuffer and then adds the current value of the loop. Finally, it calls toString to buffer and assigns the result to the existing String reference.

The WITHSTRINGBUFFER method is exactly the opposite of this method. When WITHSTRINGBUFFER is called only the Append method of the existing StringBuffer, no new String reference is created.

In this case, we already know that using StringBuffer instead of String is a good practice, but if we don't know? Then javap can help us find the answer. Here you can see more detailed explanations about String, StringBuffer

You will not often need a Java reverse pollier, but when you need it, you know that your own machine has one and the use of fairly simple reverse program is of course a good thing. If you are interested, read a book to see other options of Javap - Maybe you will find the features you need in your environment.

-------------------------------------------------- ------------------------------ author: David Petersheim is an application development director Genscape company. He designs and develops server-side applications to get and process live energy data.

转载请注明原文地址:https://www.9cbs.com/read-118872.html

New Post(0)