String... args 和 String[] args 的区别
1 public static void main(String[] args) { 2 callMe1(new String[] { "a", "b", "c" ,"d"}); 3 callMe2("a", "b", "c" ,"d"); 4 // You can also do this 5 // callMe2(new String[] {"a", "b", "c"}); 6 } 7 8 public static void callMe1(String[] args) { 9 System.out.println(args.getClass() == String[].class); 10 for (String s : args) { 11 System.out.println(s); 12 } 13 } 14 15 public static void callMe2(String... args) { 16 System.out.println(args.getClass() == String[].class); 17 for (String s : args) { 18 System.out.println(s); 19 } 20 }
输出结果:
true a b c d true a b c d
方法一是传统的参数类型:字符串数组类型;
方法二是可以传递一个或多个string类型的参数,不限制参数个数。