Java数组约定:String [] args vs. String args [] [英] Java array convention: String[] args vs. String args[]

查看:175
本文介绍了Java数组约定:String [] args vs. String args []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在教授学生作为导师编程惯例。我告诉他们,他们可以在 Oracle代码规则中找到大多数公约。

I am currently teaching students as a tutor programming conventions. I've told them that they can find most conventions in the Oracle Code Conventions.

在我上一个教程中,学生询问是否:

In my last tutorial a student asked if:

public static void main(String args[])

public static void main(String[] args)

按惯例写或如果有差异。我从来没有见过第一个版本,所以我很确定第二个是一个约定。

is written by convention or if there is a difference. I have never seen the first version before, so I'm very sure that the second one is a convention. But I don't have a source for that.

你能给我一个源代码(最好是从oracle,像我上面链接的页面)

Can you give me a source (preferably from oracle, like the page I've linked above) that makes clear which of both is convention?

我知道两者都是约定表达式是等效的:

I know that both expressions are equivalent:

JLS 7 ,p。 292状态:

The JLS 7, p. 292 states:

An array type is written as the name of an element type followed 
by some number of empty pairs of square brackets []. 

293:

The [] may appear as part of the type at the beginning of the declaration, 
or as part of the declarator for a particular variable, or both.

For example:
    byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
    byte rowvector[], colvector[], matrix[][];

但这对惯例不起作用。

因此,它们是相同的(不是规范,但这里是一个)。
他们在一个小例子中产生相同的字节码,所以我非常确定它们在实践中也是一样的。

So they are identical (not specs, but here is a source). They produce the same bytecode in a small example, so I'm very sure that they are also identical in praxis.

推荐答案

这不是来自Oracle,但我认为它会有帮助。

This is not from Oracle but I think it will help.

这是来自Kathy Sierra的书 SCJP Sun Certified Programmer for Java 6

It is from Kathy Sierra's book SCJP Sun Certified Programmer for Java 6

int[] key;
int key [];

当声明数组引用时,应立即将数组括号
后声明的类型,而不是后面的标识符(变量
name)。这样,任何阅读代码的人都可以很容易地知道,例如,key是对一个int数组对象的
引用,而不是一个int原语。

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, key is a reference to an int array object, and not an int primitive.

这篇关于Java数组约定:String [] args vs. String args []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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