public static void main(String[] args)中String[] args的作用
2013-10-27 20:22
555 查看
String[] args:这是用来接收命令行传入的参数,String[]声明args是可以用来存储字符串数组的。换一种说法就是参数args的主要作用是为程序使用者在命令行状态下与程序交互提供了一种手段。如下例子:
public class ArgsDemo {
public static void main(String[] args) {
String str = new String();
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
str += args[i];
}
System.out.println(str);
}
}
运行该程序,java ArgsDemo a b c,将得到这样的输出:
a
b
c
abc
public class ArgsDemo {
public static void main(String[] args) {
String str = new String();
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
str += args[i];
}
System.out.println(str);
}
}
运行该程序,java ArgsDemo a b c,将得到这样的输出:
a
b
c
abc