我知道当我们使用下面给出的字符串文字时,String对象在字符串池中创建(如果它不存在)。将为String创建多少个对象?
String str1= "hello";
String str2= "hello";
在上面的例子中,只有一个字符串对象将在池中创建。
但是,当我们使用new关键字它总是在堆内存中创建一个新的String对象(即使有一个在字符串池)
String str3=new String("hello"); // here a new object will be created in heap.
在这里,我有一个关于许多对象如何将一个混乱在下面的情况下创建(池或堆内存)。
1) String s="Hello";
String s1 = new String ("Hello");
2) String s = new String("Hello");
String s1 = new String("Hello");
3) String s="Hello";
String s1=new String (s);
4) String s1 = new String ("Hello");
String s="Hello";
您是否试过阅读JRE源代码? –