简体   繁体   中英

When is interning during done during compile time or run-time? The reason for such behaviour in code? Problems in BlueJ?

输入为“ 12”当您输入“ 12”时,它将返回true当您在此代码中输入“ 012”时,它将返回false。 Value of string is 12 .And 'string_input' store the input number string is declared and initialized in the code while string_input is inputed by the user during run-time

Case 1

string_5=string_input.substring(0);
System.out.println(string==string_5);

when we input "12" it returns true

Case 2

string_5=string_input.substring(1);
System.out.println(string==string_5);

and now when we input "012".. it returns false

Why do this happen??? :/

To answer the title question: Technically runtime only, as during compile time there is no String pool to intern into.

However, all string literals and constant-valued strings as defined in the source files are automatically interned when the program starts up, and additional strings can be interned using the String#intern() method.

Also, for future readers: OP's results appear to be an IDE quirk, out of all things. OP was originally entering input as a command line argument through BlueJ's custom public static void main(String string_input) main method signature. Apparently BlueJ does some shenanigans behind the scenes to make this match the public static void main(String[] args) signature required by the JLS, and apparently those shenanigans involve interning the input string at some point, leading to OP's results.

Edit for below answer: Looks like I made a minor reading mistake. While this answer somehow (sort of) works, it relies on the fact that string == string_input , which I cannot confirm until OP updates the question (which apparently cannot be done as the source code has been deleted).

The answer can be found in the source code for String :

public String substring(int beginIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    int subLen = value.length - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}

Notice the last line: if beginIndex == 0 , the original String itself is returned. Thus, in Case 1, string is indeed pointing at the same object as string_5 , and so true is printed.

However, if beginIndex != 0 , a new String is created, and so string is no longer pointing at the same object as string_5 , and so false is printed.

When is interning during done during compile time or run-time?

Interning is done at runtime, when you call String.intern().

However there is another process, called 'constant pooling', that happens at compile time. All string literals that are equal are reduced to a single entry in the object code for a class.

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