数组声明的区别 [英] Difference between array declaration
问题描述
int[] a = new int[]{1,2,3};int[] b = {1,2,3};
a 和 b 有什么区别?我是否正确,第一个是对象,'a' 是一个链接,第二个是原始类型,'b' 是一个变量?但是第一个数组有什么优点/缺点?
在像您这样的初始化中,它们之间根本没有区别.它们产生相同的字节码.请注意,您必须在作业中使用第一种形式:
int[] b;b = {1,2,3};//<== 语法错误
<块引用>
第一个是对象,'a' 是一个链接,第二个是原始类型,'b' 是一个变量,我对吗?
不,在这两种情况下,您都有一个变量(a
、b
),它是对数组的引用.
int[] a = new int[]{1,2,3};
int[] b = {1,2,3};
What difference between a and b? Am I right that first is object and 'a' is a link, and second is a primitive type and 'b' is a variable? But what advantages/disadvantages have first array?
In an initialization like you have there, there is no difference between them at all. They result in the same bytecode. Note that you have to use the first form in an assignment, though:
int[] b;
b = {1,2,3}; // <== Syntax error
Am I right that first is object and 'a' is a link, and second is a primitive type and 'b' is a variable?
No, in both cases, you have a variable (a
, b
) which is a reference to the array.
这篇关于数组声明的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!