gpt4 book ai didi

java - 当字符串实习生()方法被调用时

转载 作者:行者123 更新时间:2023-12-03 20:38:07 26 4
gpt4 key购买 nike

Case 1:

  String str = "StackOverFlow";
String str1 = "StackOverFlow";
if(str==str1){
System.out.println("equal");//prints equal
}

Case 2:

  String str = "StackOverFlow";
String str1=str.intern();
if(str==str1){
System.out.println("equal");//prints equal
}


跟进问题:
  • 我想知道对于第一种情况 JVM 是否调用 intern()在内部分配 str 的引用至str1 ?
  • 在第一种情况下两个引用如何相等?
  • 第一种情况是否意味着每当您声明像 String str = "StackOverFlow"; 这样的字符串时它与 intern() 一样添加到字符串池中方法?
  • String str = "StackOverFlow"; 使用的字符串池和 intern()是在堆外分配的吗?如果是,具体在哪里?


  • 对于问题 4,答案如下:

    在 Java 6 及更早版本中,实习字符串也存储在永久代中。在 Java 7 中,interned 字符串存储在主对象堆中。

    这是文档所说的:

    In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.



    更多细节来自这里:

    Java 6 中的 String.intern()

    In those good old days all interned strings were stored in the PermGen – the fixed size part of heap mainly used for storing loaded classes and string pool. Besides explicitly interned strings, PermGen string pool also contained all literal strings earlier used in your program (the important word here is used – if a class or method was never loaded/called, any constants defined in it will not be loaded).

    The biggest issue with such string pool in Java 6 was its location – the PermGen. PermGen has a fixed size and can not be expanded at

    runtime. You can set it using -XX:MaxPermSize=96m option. As far as I know, the default PermGen size varies between 32M and 96M depending on the platform. You can increase its size, but its size will still be fixed. Such limitation required very careful usage of String.intern – you’d better not intern any uncontrolled user input using this method. That’s why string pooling at times of Java 6 was mostly implemented in the manually managed maps.



    Java 7 中的 String.intern()

    Oracle engineers made an extremely important change to the string pooling logic in Java 7 – the string pool was relocated to the heap. It means that you are no longer limited by a separate fixed size memory area. All strings are now located in the heap, as most of other ordinary objects, which allows you to manage only the heap size while tuning your application. Technically, this alone could be a sufficient reason to reconsider using String.intern() in your Java 7 programs. But there are other reasons.

    最佳答案

    引用是相等的,因为它们都是字符串文字。
    intern()调用str不需要,因为它也是文字。何时需要使用 intern() 的示例(顺便说一下,它比 equals() 慢得多,所以不要使用它)将是在构造带有字节或字符数组的字符串时。

    例如:

    final String str1 = "I am a literal";
    final String str2 = new String(str1.toCharArray());

    final boolean check1 = str1 == str2; // false
    final boolean check2 = str1 == str2.intern(); // true

    关于java - 当字符串实习生()方法被调用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993255/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com