数组声明的区别 [英] Difference between array declaration

查看:50
本文介绍了数组声明的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int[] a = new int[]{1,2,3};int[] b = {1,2,3};

a 和 b 有什么区别?我是否正确,第一个是对象,'a' 是一个链接,第二个是原始类型,'b' 是一个变量?但是第一个数组有什么优点/缺点?

解决方案

在像您这样的初始化中,它们之间根本没有区别.它们产生相同的字节码.请注意,您必须在作业中使用第一种形式:

int[] b;b = {1,2,3};//<== 语法错误

<块引用>

第一个是对象,'a' 是一个链接,第二个是原始类型,'b' 是一个变量,我对吗?

不,在这两种情况下,您都有一个变量(ab),它是对数组的引用.

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屋!

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