Java的:为什么不能返回数组使用{..}没有新的运营商? [英] Java: Why can't return array using {..} without new operator?

查看:153
本文介绍了Java的:为什么不能返回数组使用{..}没有新的运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索网站上的很多,但没有发现任何相关的问题。所以,我认为这是不是重复。

I have searched a lot on the website, but didn't find any related question. So I believe it is not a duplicate.

我知道我们可以用3种方式初始化数组:

I know we can initialize an array with 3 ways:


  • 的char [] = charAr新的char [10];

  • 的char [] charAr = {'A​​','B','C'};

  • 的char [] = charAr新的char [] {'A','B','C'};

  • char[ ] charAr=new char[10];
  • char[ ] charAr={'a', 'b', 'c'};
  • char[ ] charAr=new char[]{'a','b','c'};

1的问题是:什么是的名称{'A','B','C'} 样的东西?它被称为数组常量?

1st question is: what is the name of {'a','b','c'} kind of stuff? Is it called array literals?

第二个问题是:什么区别新的char [] {'A','B','C'} { 'A','b','C'}

2nd question is: what is the difference between new char[]{'a','b','c'} with {'a','b','c'}?

第三个问题是:为什么我不能返回含有新建的数组{'A','B','C'}
我不得不返回新的char [] {'A','B','C'}

3rd question is: why I can't return a newly created array with {'a','b','c'}? I have to return new char[]{'a','b','c'}.

第四问题:既新的char [10] 新的char [] {'A','B','C'} 是数组构造函数,对吧?

4th question: both new char[10] and new char[]{'a','b','c'} are constructors of array, right?

推荐答案

在声明中:

char[ ] charAr={'a', 'b', 'c'};

阵列 {'A','B','C'} 被称为数组初始化的。它在Java语言规范的 10.6节描述。它只能写成这样(没有新的char [] 在它前面)作为变量声明的一部分。

the array {'a', 'b', 'c'} is called an array initializer. It is described in Section 10.6 of the Java Language Specification. It can only be written like that (without the new char[] in front of it) as part of a variable declaration.

在任何其他地方(包括收益语句),您需要使用的创建阵列前pression 的。这些在JLS 的 15.10节描述。您可以使用数组初始化或数组创建前pression当它被声明为初始化变量。这就是为什么新的char [] 出现在声明的变量是可选的。

In any other place (including a return statement), you need to use an array creation expression. These are described in Section 15.10 of the JLS. You can use either an array initializer or an array creation expression to initialize a variable when it is declared. That's why the new char[] appears to be optional when you declare the variable.

关于你4 问题:它们在技术上不是构造(其具有在Java中的术语具有特定的含义),但是,它们都构建阵列(或者更准确地,它们都创建阵列)。

Regarding your 4th question: they technically are not "constructors" (which has a specific meaning in Java terminology) but yes, they both construct arrays (or, perhaps more accurately, they both create arrays).

这篇关于Java的:为什么不能返回数组使用{..}没有新的运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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