Number of objects created when using String intern method in Java

前端 未结 3 1029
我在风中等你
我在风中等你 2021-01-27 09:38

I understand String\'s intern method.

String s1 = \"Hello\";             
String s4 = new String(\"Hello\").intern();  

Output of (s1 ==

3条回答
  •  自闭症患者
    2021-01-27 10:14

    You should use intern when you need to optimize your code, because comparing string by reference is faster.

    As for your statement, only two object will be created.

    Note that too much use of intern may cause to a memory exception as they are stored in the PermGen which is normally small, so make sure you configure correctly your JVM.

提交回复
热议问题