JAVA中String s = "hello"和String s = new String("hello")有什么区别啊?

2025-05-13 02:17:04
推荐回答(5个)
回答1:

存放地方的区别:
第一个s是一个字符串常量,它存放在内存的静态存储区。
第二个s是一个对象,它是存放在堆中的。
用法的区别:
第二个是个对象,所以可以用String类中的所有方法,而第一个不是对象,它不可以操作String类中的方法

回答2:

晕倒,没什么大的区别
第一个s是一个字符串常量,它存放在内存的静态存储区。
第二个s是一个对象,它是存放在堆中的。

用法是一样的。

回答3:

String s = "hello"存放在内存中的字符串常量区,
String s = new String("hello")在堆区创建了一个对象

回答4:

String s = "hello" 创建了两个对象: s 和 hello
String s = new String("hello")三个 s String对象 和 hello

回答5:

二楼的说的比较全面 就是这个样子