简体   繁体   中英

Intellij evaluator strange behavior about string pool

I'm using Intellij IDEA to learn about java string pool, but I think I faced with an strange thing.

The Problem is:
When I'm running following code snippet, everything is ok and b is true . At line four I put a breakpoint.

        String str1 = "abc";
        String str2 = "abc";
        boolean b = (str2 == str1);
        System.out.println(b); // b is true

At this place I check following code in the Evaluate window of Intellij but the result was strange for me, because b2 is false this time.

        String str3 = "abc";
        boolean b2 = (str2 == str3);

I expect b2 becomes true because of "abc" string is in string pool before str3 be declared.
Can anyone explain why this happened.
Thanks in advance.

The IntelliJ evaluator uses its own logic for evaluating expressions and does not use the string pool in the same way as the underlying JVM. You should not use it to understand how the JVM works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM