INT []和Integer []数组 - 的区别是什么? [英] int[] and Integer[] arrays - What is the difference?

查看:218
本文介绍了INT []和Integer []数组 - 的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下基本知识:


  1. 任何对象只生活堆,

  2. 数组是A 对象

  3. 整数 IS-A 对象

  1. Any Object lives only on heap,
  2. Array IS-A Object and
  3. Integer IS-A Object

我发现自己很难回答这样一个简单的问题:是否有 INT [] 整数[] 的JVM ?或者,它才有意义,在编译时?

I find myself difficult to answer such a simple question: Is there any difference between int[] and Integer[] inside of JVM? Or it makes sense only at "compile-time"?

推荐答案

有是在运行时的差异。

INT [] 是原始的int类型的数组。 整数[] 是一个对象数组,拿着引用Integer对象。

int[] is an array of primitive int values. Integer[] is an "object" array, holding references to Integer objects.

最重要的实际区别: INT [] 不能持有

Most important practical difference: int[] cannot hold null values.

但我仍然困惑:确实 INT [] 商店只是一种原始的价值观?如果是这样的 - 没有它意味着原始类型可以堆生活在没有被包裹?

But I'm still confused: does int[] store just a primitive values? If so - doesn't it mean that primitive types can live on heap without being wrapped?

INT [] 确实店基本类型。和数组本身住在堆。然而,这些原语被分配作为阵列的一部分。他们不是单独存放在其他地方堆。这非常类似于原始字段如何是一个对象实例的一部分:该对象是在堆中,其场是该对象的一个​​组成部分(而对于非原始场,仅参照存储在对象内和目标实例参考点在上堆分开存放)。

int[] does store primitive types. And the array itself lives on the heap. However, those primitives are allocated as part of the array. They are not stored separately elsewhere on the heap. This is very similar to how a primitive field is part of an object instance: The object is on the heap, and its field is an integral part of that object (whereas for a non-primitive field, only the reference is stored inside the object and the target instance that reference points at is stored separately on the heap).

您可以说, INT 被包装的数组中。

You could say the int is "wrapped" inside the array.

这篇关于INT []和Integer []数组 - 的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆