ITeye博客

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

HelloTommy · 2011-03-29 20:26 · 蛋蛋疼就扯扯 · 编程语言 · Markdown

这几天学习下几个框架的源码,有很多 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类型的参数,不限制参数个数。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。