为什么说 String s= new String("hello"); 会创建两个对象

String类:为什么说 String s= new String("hello"); 会创建两个对象,这里只new了一次啊

这里

JVM就会在字符串池中创建一个“hello”的对象,然后new String又会在堆中创建一个对象

String s="hello";是一个对象
String s= new String("hello");显然就是两个了。只是这里作为参数的hello没有变量名

你分成两步就能看出来喽

new的时候在队里创建了对象;串池中也会创建一个“hello”对象。