New String vs String s ="something",有什么区别?

JAN*_*JAN 0 java string new-operator

考虑一下代码:

public class Strings {

    public static void createStr()
    {
        String s1 = new String("one");
        String s2 = "one";

        System.out.println(s1);
        System.out.println(s2);
    }

    public static void main(String args[])
    {
        createStr();
    }

}
Run Code Online (Sandbox Code Playgroud)

String s1 = new String("one");和之间有什么区别String s2 = "one";

The*_*ind 7

  String s1 = new String("one"); // will be created on the heap and not interned unless .intern() is called explicityly. 
  String s2 = "one";  // will be created in the String pool and interned automatically by the compiler.
Run Code Online (Sandbox Code Playgroud)