Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
String str = "Hello"; str = "Hello";
以上语句将在堆上创建两个对象,还是 jvm 返回相同的对象?提前致谢
String str = "Hello" 它将进入 SCP 内存。比如 str 指向你的字符串变量“hello”。只有一个对象。
字符串文字存储在 SpringPool 中。
在上面的示例中,“Hello”是一个字符串池,并且该对象在字符串池中只有一个引用。因此,内存中只有一个对象。