作者jtmh ()
看板java
标题Re: [J2SE] 这竟然是合法的 Java 语法...
时间Sun Nov 2 03:40:00 2008
※ 引述《macbuntu (邀怪)》之铭言:
: ※ 引述《macbuntu (邀怪)》之铭言:
: : 刚刚看到这个写法真是大惊, 这竟然是合法的语法:
: : public int func()[] {
: : return new int[] { 1, 2, 3 };
: : }
: 刚刚玩了一下, 发现还可以混用:
: public int[][] func() { ... }
: public int[] func() [] { ... }
: public int func() [][] { ... }
: 上面这三种写语意一模一样, 都是回应 int[][]
: 尤其第二种, 那样写实在是太超过了 Orz
在 Java Language Spec 中找到说明了:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#38703
"
For compatibility with older versions of the Java platform,
a declaration form for a method that returns an array is
allowed
to place (some or all of) the empty bracket pairs that form
the declaration of the array type
after the parameter list. This is
supported by the obsolescent production:
MethodDeclarator:
MethodDeclarator [ ]
but
should not be used in new code."
一切都是为了与旧版相容,
但老话一句,可以那样用不代表就真的应该那麽用。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.175.148.198