【问题标题】:Where the Local object Will be Stored of String in java本地对象将存储在java中的字符串的位置
【发布时间】:2014-11-03 16:54:36
【问题描述】:

好吧,我正在参考 Java 的一些基本原理,当我遇到堆栈和堆时。 据我了解,堆是主内存,其中实例表示类的对象将被存储,类变量将被存储。

查看存储所有方法和局部变量和方法的堆栈。

现在我的困惑是,如果我在方法及其类 String 的实例中本地创建任何类对象,比如 String 对象,那么它可以存储在哪里。我没有得到正确的结论,任何人都可以提出建议。

如果这可能与其他任何重复,我会道歉,因为我找不到类似的,所以为了澄清我的理解,我需要帮助。

这里是工作示例。

public class CreatingLcoalString {
      public void methodstring(){

          String s = new String("This is the area of confustion"); // this is the area of confustion
          System.out.println(s);
      }

      public static void main(String argsp[]){

          new CreatingLcoalString().methodstring();
      }
}

【问题讨论】:

标签: java string heap-memory


【解决方案1】:

references 将存储在堆栈中。它将指向 heap 上的 String 对象。 (忽略逃逸分析)。

在您的情况下,将创建 2 个 String 对象。一个在堆上,另一个在字符串常量池(堆的一部分)中。

"This is the area of confustion" will be present in 2 places. Both are on the heap.

PS : 转义分析可能导致字符串存储在堆栈中。它是自 jdk 6 update 23 以来引入的特殊优化机制。 更多信息herehere

【讨论】:

  • 感谢ans但忽略逃逸分析?你能告诉我吗?它是什么?
猜你喜欢
  • 2011-11-29
  • 1970-01-01
  • 2011-08-21
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
相关资源
最近更新 更多