为什么`Integer [100] arr;`无效,而`Ineger [] arr;`有效? [英] Why `Integer[100] arr;` is invalid, while `Ineger[] arr;` is valid?
问题描述
我试图为100个项目分配一个Integer数组,为什么此声明在Java中无效?
I was trying to allocate an Integer array for 100 items, why this declaration isn't valid in Java?
Integer[100] intArr1; ----- (1)
这是有效的:
Integer[] intArr; ----- (2)
由于(2)是有效的,它占用多少内存?任何人都可以帮忙解释一下.
As (2) is valid, how much memory does it occupy? Can anyone help to explain a bit.
SO中有些问题与我的问题相似,但是它们并不相同,因此我在问这个问题之前确实先看过SO.
推荐答案
第一个无效,因为这不是正确的Java语法.
The first one isn't valid because that's not proper Java syntax.
第二个占用0的内存,因为您尚未创建数组.仅引用数组,引用为 null
.
The second one occupies 0 memory, as you haven't created an array. Only a reference to an array, with the reference being null
.
您需要在那里的 new Integer [100]
才能真正创建 array对象.
You need a new Integer[100]
there to actually create the array object.
这篇关于为什么`Integer [100] arr;`无效,而`Ineger [] arr;`有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!