Find interesting insider strings through Java bytecode (top)

Source: Internet
Author: User

Original Source: Jaffa

Many times when we write Java code, judge and guess the code problem is mainly through the results of the operation to get the answer, this blog is mainly to use Java bytecode way to further verify what we know. Java bytecode knowledge is not introduced here, if you want to know more Java bytecode or interested in the friend can first read the bytecode base: the JVM bytecode exploration.

The string literal can be judged by ' = = ' Whether the two strings are the same, because everyone knows that ' = = ' is used to determine whether the value of the two object reference address is consistent, two values of the same string literal definition point to the same value memory address it? The answer is yes.

12345678910 package com.jaffa.test.string;public class ConstPoolTest {    public static void main(String[] args){        String str1 = "strVal_1";        String str2 = "strVal_1";        //print str1==str2 is true        System.out.printf("str1==str2 is %b",str1==str2);    }}

The literal value of str1 and STR2 declared in the code is Strval_1, and STR1==STR2 is true, indicating that both str1 and STR2 variables point to the memory address of the same string constant value, and the result is validated by Java bytecode.

At the command line we use the JAVAP tool to view the byte code of a class file.

1 javap -v com.jaffa.test.string.ConstPoolTest

In the constant pool list, you see that #16 is a string type and the value points to #17, and #17 is a UTF8 character set encoding value Strval_1, so #16 and #17 are ultimately expressed in a constant pool with a string value of Strval_ Constant data of 1.

Then you need to confirm that str1 and str2 two variable values are all pointing to #16?

12345 //ldc表示将一个常量加载到操作数栈 0: ldc           #16//将#16对应的常量值加载到操作数栈中             2: astore_1              //将当前操作数栈中赋于变量1,即str1 3: ldc           #16 //再次将#16对应的常量值加载到操作数栈中 5: astore_2              //将当前操作数栈中赋于变量2,即str2

From the above bytecode execution, both STR1 and STR2 are assigned to the same constant value, which results in two changes pointing to the same memory address.

In the same way, let's take a look at what happens if the non-literal situation is, the Java code is as follows:

12345678910 package com.jaffa.test.string;public class ConstPoolTest {    public static void main(String[] args){        String str1 = "strVal_1";        String str2 = new String("strVal_1");        System.out.printf("str1==str2 is %b",str1==str2);    }}

The above code output is false, through JAVAP view found STR2 variable bytecode instruction has changed, the following two:

12345678 //ldc表示将一个常量加载到操作数栈 0: ldc           #16//将#16对应的常量值加载到操作数栈中             2: astore_1              //将当前操作数栈中赋于变量1,即str1 3: new#18//创建了一个类实例,#18指向一个实例类型String 6: dup                   //配置上行完成操作栈指令 7: ldc           #16//将#16对应的常量值加载到操作数栈中  9: invokespecial #20//调用String实例初始化方法,并将#16输入12: astore_2              //将new出来的实例赋于变量2,即str2

At this point the STR2 variable is creating a new memory address instead of pointing directly to the #16 constant memory address, so the result of STR1==STR2 is false. It is also possible to see more bytecode instruction operations through the new String (), and more system resources are spent on the run.

This series:
    • Find interesting insider strings through Java bytecode (top)
    • Find interesting insider strings through Java bytecode (middle)
    • Discover interesting insider initialization through Java bytecode (bottom)

Http://www.importnew.com/18785.html

Find interesting insider strings through Java bytecode (UP) (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.