Home > Java > javaTutorial > body text

Detailed explanation of using javap to analyze Java string operations

不言
Release: 2018-10-22 15:23:26
forward
2066 people have browsed it

This article brings you a detailed explanation of using javap to analyze Java string operations. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Let’s look at this line of Java code for a simple string assignment operation.

String a = "i042416";
Copy after login

Use the command line to decompile the Java class containing this line of code to view its bytecode:

javap -v constant.ConstantFolding

Detailed explanation of using javap to analyze Java string operations

We see that the string "i042416" was added to the constant pool by the Java compiler.

Detailed explanation of using javap to analyze Java string operations

##Java code String a = "i042416" is translated into the following two sentences of bytecode:

Detailed explanation of using javap to analyze Java string operations

ldc #16: First, the underlying native method of JVM, StringTable::intern, is called to generate the internal storage implementation of String, char[]. Then execute ldc #16 to load the constant code-named #16 in the constant pool onto the stack, i.e. i042416.

2. astore_1: Store the reference of "i042416" into the local variable with serial number 1 (that is, local variable a in our code).

Let’s look at a slightly more complex example.

Do a string splicing operation.

String aa1 = "i042416";
String aa2 = "jerrywang";
String aa3 = "i042416" + "jerrywang";
Copy after login

You can see that during the compilation phase, the compiler byte splices the values ​​​​of the two string constants, and the result "i042416jerrywang" is stored in the variable aa3, as a new string constant, in the constant The codename in the pool is #21.

Detailed explanation of using javap to analyze Java string operations

Detailed explanation of using javap to analyze Java string operations## Therefore, the variables aa1 and aa3 actually point to the constant pool The same constant in , so direct comparison with == will also return true.

String aa1 = "i042416jerrywang";
String aa2 = "jerrywang";
String aa3 = "i042416" + "jerrywang";
System.out.println(aa1 == aa3);
Copy after login

Detailed explanation of using javap to analyze Java string operations

The above is the detailed content of Detailed explanation of using javap to analyze Java string operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template