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.
}
}
Run Code Online (Sandbox Code Playgroud)
在前面的代码片段中,除最后一个之外的所有表达式都被编译并运行正常.虽然最后一个看起来类似于其立即语句的语句,但编译器发出编译时错误 - 非法启动表达式 - 而不是语句.为什么?