String... args 和 String[] args 的区别

时间:2023-03-09 16:09:02
String... args 和 String[] args 的区别
 public static void main(String[] args) {
callMe1(new String[] { "a", "b", "c" ,"d"});
callMe2("a", "b", "c" ,"d");
// You can also do this
// callMe2(new String[] {"a", "b", "c"});
} public static void callMe1(String[] args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
} public static void callMe2(String... args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}

输出结果:

true
a
b
c
d
true
a
b
c
d

  

方法一是传统的参数类型:字符串数组类型;

方法二是可以传递一个或多个string类型的参数,不限制参数个数。