What is the purpose of Java String.intern ()?

I know that there are two ways to create a String in Java:

String a = "aaa";
String b = new String("bbb");

With the first method, Java will definitely create a String object in the string pool and access it a. (Assume that "aaa" wan't in the pool before.)

In the second method, the object will be created on the heap, but will jvm create the object in the string pool?

In this post, Questions about the Java string pool , @Jesper said:

If you do this:

String s = new String("abc");

then there will be one String object in the pool, one that represents the literal "abc",> and there will be a separate String object, and not in the pool, which contains a copy> of the contents of the merged object.

, new String("bbb"); "bbb", , java . intern()? http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern() :

intern, , String, equals (Object), . String String.

, , , ? ?

+4
4

String :

  • , "bbb".
  • intern.

intern , , . :

String bb = "bbb".substring(1); // substring creates a new object

System.out.println(bb == "bb");          // false
System.out.println(bb.intern() == "bb"); // true

:

System.out.println(new String("bbb").intern() == "bbb"); // true

new String("bbb") ...

String fromLiteral = "bbb";                     // in pool
String fromNewString = new String(fromLiteral); // not in pool

... . , "bbb" :

String [...].

, String.

new String(...) .

String . :

  • String, - . (, , ..)
  • - , Reader.
  • , .

intern , . , , :

  • ==.
  • , .
+2

, String Java, , - . JVM , . , String, JVM , , . String .

, String /.

String str = "bbb";

String

String str = new String("bbb"));

.

API intern String String literal, String new. , . str3 new, intern, JVM literal.

public class StringInternExample {

    public static void main(final String args[]) {

        final String str = "bbb";
        final String str1 = "bbb";
        final String str2 = new String("bbb");
        final String str3 = new String("bbb").intern();

        System.out.println("str == str1 : "+(str == str1));
        System.out.println("str == str2 : "+(str == str2));
        System.out.println("str == str3 : "+(str == str3));
    }
}

:

str == str1 : true
str == str2 : false
str == str3 : true

:

: http://ourownjava.com/java/java-string-immutability-and-intern-method/

+6

, new String("abc") , . , 5 Java, " ", :

, , :

String s = new String("stringette"); // DON'T DO THIS!

String , , . constructor ( "stringette" ) String, , . , . :

String s = "stringette";

String, , . , , , , [JLS, 3.10.5].

http://uet.vnu.edu.vn/~chauttm/e-books/java/Effective.Java.2nd.Edition.May.2008.3000th.Release.pdf

+1

, jvm ?

, "bbb", 1. , - .

, ( "bbb" ); "bbb", , java . ()?

. , .

, , , ?

String.intern, .

Although I would recommend using a specialized collection for such cases, interning can be useful where it can be used to avoid creating additional duplicate objects . In some use cases where interning can be useful - as in, the same string value can appear many times - found in JSON keys and XML element / attribute names.


1 This is trivial for the mind, consider:

String _b = "bbb";          // string from string literal (this is interned)
String b = new String(_b);  // create a NEW string via "copy constructor"
b == _b           // -> false (new did NOT return an interned string)
b.equals(_b)      // -> true  (but it did return an equivalent string)
b.intern() == _b  // -> true  (which interns to .. the same string object)
+1
source

Source: https://habr.com/ru/post/1532265/


All Articles