一个JAVA 的MAIN方法引发的一场血案
Q: What if the main method is declared as private?
A: The program compiles properly but at run time it will give "Main method not public." message.
Q: What if the static modifier is removed from the signature of the main method?
A: Program compiles. But at run time throws an error "NoSuchMethodError".
Q: What if I write static public void instead of public static void?
A: Program compiles and runs properly.
Q: What if I do not provide the String array as the argument to the method?
A: Program compiles but throws a run time error "NoSuchMethodError".
Q: What is the first argument of the String array in main method?
A: The String array is empty. It does not have any element. This is unlike C/C++(读作plus plus) where the first element by default is the program name.
Q: If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
A: It is empty. But not null.
Q: How can one prove that the array is notnull but empty using one line of code?
A: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
仔细看完后有人直接吐血了,拿个eclipse,这几个问题全部模拟一边就可以了,即无算法也不需要死记硬背
有人会说了,唉,我怎么写了5年的JAVA怎么就没记得多写多看,多想想这个public static void main(String[] args)方法呢?唉。。。 |
|