Java int[] 和 Integer[] 数组 - 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18845289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int[] and Integer[] arrays - What is the difference?
提问by Ilya Gubarev
Considering following basics:
考虑以下基础:
- Any
Object
lives only on heap, - Array IS-A
Object
and Integer
IS-AObject
- 任何
Object
只生活在堆上, - 阵列 IS-A
Object
和 Integer
IS-AObject
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"?
我发现自己很难回答这样一个简单的问题:JVM之间int[]
和Integer[]
内部有什么区别吗?还是仅在“编译时”才有意义?
采纳答案by Thilo
There is a difference at run-time.
运行时存在差异。
int[]
is an array of primitive int values. Integer[]
is an "object" array, holding references to Integer objects.
int[]
是原始 int 值的数组。Integer[]
是一个“对象”数组,保存对 Integer 对象的引用。
Most important practical difference: int[]
cannot hold null
values.
最重要的实际区别:int[]
不能保持null
值。
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
被“包裹”在数组中。
回答by Sajal Dutta
Firstly, Integeris a class/object while intis a primitive type. Integer is a wrapperfor int. If you need a nullvalue to be stored, or need to use collection, use Integer. You can do-
首先,Integer是一个类/对象,而int是一个原始类型。Integer 是int的包装器。如果您需要存储空值,或者需要使用collection,请使用 Integer。你可以做-
List<Integer> integerList = new ArrayList<Integer>();
So, an array of primitive types are different from an array of integer objects.
因此,原始类型数组不同于整数对象数组。
回答by Terafor
- Integer[] is an array (object) of objects
- int[] is an array (object) of primitives
- Integer[] 是对象的数组(对象)
- int[] 是基元数组(对象)
Than there is a difference between Integer and int summarized here.
此处总结的Integer 和 int 之间存在差异。
回答by Maroun
This image should help you to understand the difference:
这张图片应该可以帮助你理解差异:
int
is a number, it's a primitive type. Integer
is an object.
int
是一个数字,它是一个原始类型。Integer
是一个对象。
When you have an array of Integer
s, you actually have an array of objects. Array of int
s is an array of primitive types.
当你有一个Integer
s数组时,你实际上有一个objects数组。的阵列int
s是原始类型的阵列。
Since arrays are objects, they're allocated on the heap. If it's an array of int
s, these int
s will be allocated on the heap too, within the array.
由于数组是对象,它们被分配在堆上。如果它是int
s的数组,则这些int
s 也将在数组中的堆上分配。
You may find this linkhelpful.
您可能会发现此链接很有帮助。