Java string intern and literal

后端 未结 5 553
太阳男子
太阳男子 2020-11-29 08:58

Are the below two pieces of code the same?

String foo = \"foo\";
String foo = new String(\"foo\").intern();
5条回答
  •  没有蜡笔的小新
    2020-11-29 09:02

    Explicitly constructing a new String object initialized to a literal produces a non-interned String. (Invoking intern() on this String would return a reference to the String from the intern table.)

    Taken from : http://javatechniques.com/public/java/docs/basics/string-equality.html

提交回复
热议问题