-----------------------------------------------------------
最佳答案:
-----------------------------------------------------------
public class HelloWorldApp
{
public static void main (String[] args)
{
String str1 = new String("hello");
String str2 = "hello";
String str3 = "hello";
System.out.println(str1 == str2);//false
System.out.println(str2 == str3);//true
}
}
********************************************************************************************
可知:用new String("hello");语句时,只在堆(heap)中创建对象,不在栈(stack)中创建对象
用"hello";语句时在栈(stack)中创建对象
********************************************************************************* **********
Java中关于String的简单而又深入的问题!
本文通过一个简单的Java程序演示了如何使用==操作符比较字符串,并解释了堆和栈中字符串对象的区别。具体展示了使用new关键字创建字符串对象与直接赋值创建字符串对象时,两者在内存中的不同表现。

被折叠的 条评论
为什么被折叠?



