【问题标题】:Where are these obects stored in memory?这些对象存储在内存中的什么位置?
【发布时间】:2020-02-07 16:03:45
【问题描述】:

我使用数据类型来声明一些值,另一方面我使用包装类的概念来存储值,那么这些对象或文字值存储在内存中的什么位置?

class raw {
  public static void main(String[] args) {
    int a = 5;
    Integer aa = new Integer(5);
    // where these values are getting stored
  }
}

【问题讨论】:

    标签: java class memory types


    【解决方案1】:

    int a = 5 存储在堆栈中,

    并且new Integer(5) 对象存储在堆中。引用变量Integer aa存储在栈内存中。

    对于每个线程,它都有一个独立的栈内存,所有这些线程共享堆内存。

    【讨论】:

      【解决方案2】:

      将包装类视为 Java 中的任何其他对象。它存储的文字值作为原始变量存储在对象中。

      如果是整数,它存储在一个 int 变量中,该变量在 Integer 类中定义为private final int value;

      看这里http://developer.classpath.org/doc/java/lang/Integer-source.html类的源码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 2021-10-17
        • 2010-11-20
        • 2021-01-17
        • 2010-10-15
        • 2011-02-21
        • 2013-04-24
        相关资源
        最近更新 更多