差异fn(String ... args)vs fn(String [] args) [英] difference fn(String... args) vs fn(String[] args)

查看:97
本文介绍了差异fn(String ... args)vs fn(String [] args)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种语法对以下内容有用:

Whats this syntax useful for :

    function(String... args)

与写作相同

    function(String[] args) 

仅在调用此方法时有所不同,或者是否涉及任何其他功能它?

with difference only while invoking this method or is there any other feature involved with it ?

推荐答案

两者之间的唯一区别就是你调用函数的方式。使用String var args可以省略数组创建。

The only difference between the two is the way you call the function. With String var args you can omit the array creation.

public static void main(String[] args) {
    callMe1(new String[] {"a", "b", "c"});
    callMe2("a", "b", "c");
    // 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);
    }
}

这篇关于差异fn(String ... args)vs fn(String [] args)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆