在 main 方法参数中添加 final 关键字工作正常。既然我修改了 Java 的标准 main 方法,为什么它没有给出任何编译器错误/异常?
public class StackOverFlow {
public static void main(final String... args) {
System.out.println("Hi");
}
}
现在看看我是否编码:
public class StackOverFlow {
public static void main (String... args) {
String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
args[0] = "hi";
System.out.println(args[0]);
args =str;
for(int i=0; i<args.length; i++) {
System.out.print(args[i]);
}
}
}
对于通过传递参数来运行程序时的上述编码:
javac StackOverFlow Nisrin
我的程序输出是
hi
我还没有收到我的答复。
现在与final关键字相同
public class StackOverFlow {
public static void main (final String... args) {
String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
args[0] = "hi";
System.out.println(args[0]);
args =str;
for(int i=0; i<args.length; i++) {
System.out.print(args[i]);
}
}
}
它给了我一个错误:args可能未分配最终参数。
因为现在我分配str给args.
这意味着我通过final在参数中添加关键字并使其在 main 方法(Java 程序的入口点)中保持不变来实现很大的不同。我正在更改主要方法的签名。那为什么我没有收到任何编译错误或运行时错误?
Cats萌萌
www说
相关分类