简体   繁体   English

字符串不可变性的困惑

[英]Confusion on string immutability

I have following code:- 我有以下代码:-

public class StaticImplementer {
    private static String str= "ABC";
    public static void main(String args[]) {
        str = str + "XYZ";
    }
}

Questions:- 问题: -

  1. Here String str is static, then where this object will be stored in memory? 这里String str是静态的,那么该对象将存储在内存中的什么位置?

  2. Since String is immutable, where the object for "XYZ" will be stored? 由于String是不可变的,因此“ XYZ”的对象将存储在哪里?

  3. Where will be final object will be Stored? 最终对象将存储在哪里?

  4. And how will garbage collection will be done? 以及如何进行垃圾收集?

1) Here String str is static, then where this object will be stored in memory? 1)在这里,字符串str是静态的,那么该对象将存储在内存中的什么位置?

Those literals will be stored in the String pool memory , no matter if the variable is declared as static or not. 无论variable是否声明为static ,这些文字都将存储在字符串池内存中
More info here: Where does Java's String constant pool live, the heap or the stack? 这里的更多信息: Java的String常量池位于哪里,堆或堆栈在哪里?

2) Since String is immutable, where the object for "XYZ" will be stored? 2)由于String是不可变的,“ XYZ”的对象将存储在哪里?

Similar to the first answer: a literal will be stored in the pool memory. 与第一个答案类似:文字将存储在池内存中。 Immutability just allows the concept of shared pool memory. 不变性仅允许共享池内存的概念。

3) Where will be final object will be Stored? 3)将最终对象存储在哪里?

According to the Java specification , concatenation of literals will end up to a literal too (since known at compilation time), stored in the pool memory. 根据Java规范 ,文字的连接也将最终存储在池内存中的文字(由于在编译时已知)。
Excerpt: 摘抄:

"This is a " +        // actually a string-valued constant expression,
    "two-line string"    // formed from two string literals

4) And how will garbage collection will be done? 4)如何进行垃圾收集?

As essence of the pool memory, they won't be garbage collected by default. 作为池内存的本质,默认情况下不会进行垃圾回收。 Indeed, if garbage collected immediately, the "shared" concept of the pool would fail. 实际上,如果立即收集垃圾,则池的“共享”概念将失败。

Here String str is static, then where this object will be stored in memory? 这里String str是静态的,那么该对象将存储在内存中的什么位置?

String str is not an object, it's a reference to an object. 字符串str不是对象,而是对对象的引用。 "ABC" , "XYZ" & "ABCXYZ" are three distinct String objects. "ABC""XYZ""ABCXYZ"是三个不同的String对象。 Thus, str points to a string. 因此, str指向字符串。 You can change what it points to, but not that which it points at. 您可以更改其指向的内容,但不能更改其指向的内容。

Since String is immutable, where the object for "XYZ" will be stored? 由于String是不可变的,因此“ XYZ”的对象将存储在哪里?

As explained in above & also by Mik378, "XYZ" is just a String object which gets saved in the String pool memory and the reference to this memory is returned when "XYZ" is declared or assigned to any other object. 如上文以及Mik378所述, "XYZ"只是一个String对象,该对象保存在String池内存中,并且在声明"XYZ"或将其分配给任何其他对象时,将返回对该内存的引用。

Where will be final object will be Stored? 最终对象将存储在哪里?

The final object, "ABCXYZ" will also get saved to the pool memory and the reference will be returned to the object when the operation is assigned to any variable. 最终对象"ABCXYZ"也将保存到池内存中,并且在将操作分配给任何变量时,引用将返回到该对象。

And how will garbage collection will be done? 以及如何进行垃圾收集?

String literals are interned. 字符串文字是intern。 As of Java 7, the HotSpot JVM puts interned Strings in the heap, not permgen. 从Java 7开始,H​​otSpot JVM将内部字符串放置在堆中,而不是permgen。 In earlier versions of Java, JVM placed interned Strings in permgen. 在Java的早期版本中,JVM将内部字符串放置在permgen中。 However, Strings in permgen were garbage collected. 但是,permgen中的字符串是垃圾收集的。 Apparently, Class objects in permgen are also collectable, so everything in permgen is collectable, though permgen collection might not be enabled by default in some old JVMs. 显然,permgen中的Class对象也是可收集的,因此permgen中的所有内容都是可收集的,尽管在某些旧的JVM中可能默认情况下未启用permgen收集。

String literals, being interned, would be a reference held by the declaring Class object to the String object in the intern pool. 被嵌入的字符串文字将是由声明的Class对象持有的对内部池中String对象的引用。 So the interned literal String would only be collected if the Class object that referred to it were also collected. 因此,只有在还收集了引用了它的Class对象的情况下,才收集实习文本字符串。

Shishir Shishir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM