Java里String s = new String("Hello")

xiaoxiao2021-02-28  11

1.堆(Heap):最大一块空间.存放对象实例和数组.全局共享.

2.栈(Stack):存放基本类型,以及对象引用.线程私有.

3.方法区(Method Area):"类"被加载后的信息,常量,静态变量存放在这里.全局共享.

String str1 = "Hello" String str2 = "Hello" String str3 = new String("Hello")str1,str2统一指向同一个堆内存地址,而str3因为new关键字会在堆中申请一块

全新的内存,来创建新对象,虽然字面还是"Hello",但它和str1与str2是完全不同

的对象,有不同的内存地址.

详细资料: https://www.zhihu.com/question/22739143

转载请注明原文地址: https://www.6miu.com/read-2450203.html

最新回复(0)