15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] +" ");
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1234
What is the result?
A. No output is produced.
123
B. No output is produced.
234
C. No output is produced.
1234
D. An exception is thrown at runtime.
Copyright Tarena Corporation,2008.All rights reserved
123
E. An exception is thrown at runtime.
234
F. An exception is thrown at rijntime.
1234
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] +" ");
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1234
What is the result?
A. No output is produced.
123
B. No output is produced.
234
C. No output is produced.
1234
D. An exception is thrown at runtime.
Copyright Tarena Corporation,2008.All rights reserved
123
E. An exception is thrown at runtime.
234
F. An exception is thrown at rijntime.
1234
java Yippee 1234
什么东东?
args就是String[0]
一个参数就是String[1]....n个参数就是String[n]...
程序运行参数String[] args从命令行java Yippee 1234 中获得,1 2 3 4作为参数传递到数组args中
java Yippee 1234 执行.class文件?结果啥也没呀。空啊~
答案应该是两个都不输出任何结果;
因为两个命令的args.length都是1,for循环根本不执行,因此没有任何结果;
java Yippee 1 2 3 4
java Yippee 1 2 3 4 第一个会在运行时报错 因为args数组为空
第二个从2开始输出 所以输出234
从答案可以看出来,是LZ把题写错了。第2行应该是“java Yippee 1 2 3 4”。
args是“java 类名”之后的部分,即使没有,args也不是null,所以没异常。
for循环从1开始,所以是2 3 4。
for循环的执行流程
for(int i=0;i<args.length;i++)
{
.....}
执行顺序:红--绿--黄--灰--绿--黄--灰...
字符串数组的元素:字符串,args是运行时的动态参数,注意是字符串数组。