我有一个问题;我不知道它是否愚蠢......但无论如何我都会问。
我们都知道当我们说
字符串名称 = "someName";
我们知道这是一个合并的字符串,所以它不会进入堆,但是......
如果我们说
整数 integer = new Integer(888);
我们知道这将成为焦点......
然后
整数 otherInteger = 444;
“otherInteger”要到哪里去?什么内存位置?
谢谢!
最佳答案
字符串名称 = "someName"; String 是 immutable 类并且 String 类型是引用或非原始类型,因此名称引用指向“someName”对象,该对象位于堆中的“String-constant-pool”中。(无堆栈). Integer 是包装类(提供将原始类型转换为对象以及将对象转换为原始类型的机制)。 代码 Integer otherInteger = 444;是自动装箱 ( https://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html ) 的示例,编译器会自动将此行转换为 Integer otherInteger = Integer.valueOf(444); .因此,Integer otherInteger= Integer.valueOf(444) 返回 Integer 对象,即它放在堆中。
关于java - 未实例化的整数将位于内存中的什么位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60937432/