在Java中,当是{A,B,C,...}阵列速记不合适的,为什么? [英] In Java, when is the {a,b,c,...} array shorthand inappropriate, and why?

查看:94
本文介绍了在Java中,当是{A,B,C,...}阵列速记不合适的,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你定义一个变量,它似乎是完全有效申报/定义一个变量如下:

If you're defining a variable, it appears to be perfectly valid to declare/define a variable as follows:

    double[][] output = {{0,0},{1,0}};

但如果你返回一个值,这似乎是无效的编写如下:

But if you're returning a value, it appears to be invalid to write the following:

    public double[] foo(){
      return {0,1,2}
    }

我本来以为在内部,这两个会一直执行相同的动作。 Eclipse的,至少,不同意。有谁知道其中的差别是什么,还有什么地方可以看到,为什么这将有利于接受前者的例子,拒绝后者?

I would have thought that internally, both of these would have been performing the same action. Eclipse, at least, disagrees. Does anyone know what the difference is, and where else it can be seen, or why it would be beneficial to accept the former example and reject the latter?

编辑:好了,所以当你初始化的合适,但我看不出有任何含糊......不能在JVM间preT从变量的名称变量的类型(在重新定义的情况下,已初始化的变量)或返回时(其中JVM可能只看该函数的返回类型)?是什么让初始化规则,禁止隐式类型的特殊情况?是什么让一般的规则需要明确的类型?

okay, so it's appropriate when you're initializing, but I don't see any ambiguity... couldn't the JVM interpret the type of variable from the name of the variable (in the case of redefining already initialized variables) or when returning (where the JVM could just look at the return type of the function)? What makes initialization a special case of a rule that would prohibit implicit type? What makes the general rule require explicit type?

推荐答案

这是一个声明中唯一可以接受的。你可以,但是,使用新的双[] {0,1,2}

It's only acceptable during a declaration. You can, however, use new double[] {0, 1, 2}.

10.6节

这是数组初始化可在声明中指定,或作为的一部分
  数组创建前pression。

An array initializer may be specified in a declaration, or as part of an array creation expression.

数组创建前pression是新的双[] {} 语法。

An array creation expression is the new double[] { } syntax.

这篇关于在Java中,当是{A,B,C,...}阵列速记不合适的,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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