Proper terminology for Object… args

女生的网名这么多〃 提交于 2019-12-03 15:43:39

问题


I'm working in Java and the typical way you specify multiple args for a method is:

public static void someMethod(String[] args)

But, I've seen another way a few times, even in the standard Java library. I don't know how to refer to this in conversation, and googling is not of much help due to the characters being used.

public static void someMethod(Object... args)

I know this allows you to string a bunch or arguments into a method without knowing ahead of time exactly how many there might be, such as:

someMethod(String arg1, String arg2, String arg3, ... etc

How do you refer to this type of method signature setup? I think it's great and convenient and want to explain it to some others, but am at a lack of how to refer to this. Thank you.


回答1:


This way to express arguments is called varargs, and was introduced in Java 5.
You can read more about them here.




回答2:


As a Keppil and Steve Benett pointed out that this java feature is called varargs.

If I'm not mistaken, Joshua Bloch mentioned that there is a performance hit for using varargs and recommends telescoping and using the varargs method as a catch all sink.

public static void someMethod(Object arg) {
    // Do something
}

public static void someMethod(Object arg1, Object arg2) {
    // Do something
}

public static void someMethod(Object arg1, Object arg2, Object arg3) {
    // Do something
}

public static void someMethod(Object ... args) {
    // Do something
}



回答3:


The Parameter method(Object... args) is called varargs. Try this link.



来源:https://stackoverflow.com/questions/16674023/proper-terminology-for-object-args

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!