package arraypkg;
import java.util.Arrays;
public class Main
{
private static void foo(Object o[])
{
System.out.printf("%s", Arrays.toString(o));
}
public static void main(String[] args)
{
Object []o=new Object[]{1,2,3,4,5};
foo(o); //Passing an array of objects to the foo() method.
foo(new Object[]{6,7,8,9}); //This is valid as obvious.
Object[] o1 = {1, 2}; //This is also fine.
foo(o1);
foo({1,2}); //This looks something similar to the preceding one. This is however wrong and doesn't compile - not a statement.
}
}
在前面的代码片段中,除最后一个之外的所有表达式都已编译并运行良好。尽管最后一条语句看起来与它的直接语句相似,但编译器会发出编译时错误 - 表达式的非法开始 - 而不是语句。为什么?