Reusability of Strings in java?

后端 未结 4 714
野趣味
野趣味 2020-12-18 22:59
String h = \"hi\";

here we are referencing string h to string literal hi. JVM has a string literal pool to store string literals so we can reuse t

相关标签:
4条回答
  • 2020-12-18 23:33

    In the case of

    String h = "hi";
    String i = "hi";
    String j = new String("hi");
    

    Depending on the version of the JDK the compiler may do what is called interning and create a single instance of the byte data that represents the "hi" and reuse it between the to variable references. In the most recent specifications all String literals are interned into the String pool in the Permanent Generation.

    Using the new keyword as in the last statement will create a new instance of the exact same bytes as a separate object.

    String objects created at runtime are not in the String pool unless .intern() is called on them. This is usually not needed and can cause problems, rarely has any significant benefits.

    h == i; // true
    h == j; // false
    j.intern();
    h == j; // true
    
    0 讨论(0)
  • 2020-12-18 23:35

    Yes, to make things simpler you can think of it as picking from same address, but to be more precise variables are holding same reference which is number/objectID which JVM use while mapping to proper memory address of object (object can be moved in memory but will still have same reference).

    You can test it with code like this:

    String w1 = "word";
    String w2 = "word";
    String b = new String("word"); // explicitly created String (by `new` operator) 
                                   // won't be placed in string pool automatically
    System.out.println(w1 == w2);  // true  -> variables hold same reference
    System.out.println(w1 == b);   // false -> variable hold different references,
                                   // so they represent different objects
    b = b.intern(); // checks if pool contains this string, if not puts this string in pool, 
                    // then returns reference of string from pool and stores it in `b` variable
    System.out.println(w1 == b);   // true  -> now b holds same reference as w1
    
    0 讨论(0)
  • 2020-12-18 23:38

    There's a String literal pool inside the JVM with all the Strings created during the lifetime of the program. By reusable, it is meant that, when you want to use another String with exactly the same characters and encoding, a new String object is not created. Instead, the reference will point to the already existing String in the string pool. This is called interning.

    This can be done because Strings are immutable in Java.

    The reason for doing something like this is to avoid the cost of creating a new String object.

    Look here for more details about what the String literal pool is and how it works: http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

    Also, look at this question: Shouldn't I do `String s = new String("a new string");` in Java, even with automatic string interning?

    0 讨论(0)
  • 2020-12-18 23:50

    What is meant is that if 20 objects use the same literal String:

    private String h = "hi";
    

    all these objects will in fact reference the same String instance in memory. And it doesn't matter, because it's impossible to change the content of the String, since it's immutable. The same instance can thus be shared without problems between objects.

    0 讨论(0)
提交回复
热议问题