About java method Object ... args parameter meaning
In reading volley source google released, suddenly saw the presence of such wording in a way, such as: v (String format, Object ... args)
I do not understand what it meant, wondering why write it this way, with the Object [] args is a mean? Why three points instead? Later, access to the documents under, it was a new JDK1.5 syntax, new features, dynamic parameter or variable parameters mean.
For example:
there is a solution v, v method call
v (how many parameters are written inside the line);
such as:
v(1,"s");
v(1,2,"s");
v("s");
When using this method the internal parameters, the Object [] args without any distinction.
But when you call, be different.
void v(Object... objs) {
}
v(obj1, obj2, obj3);
I. INTRODUCTION
In reading QueryRunner source category is encountered the following method:
public int update(String sql, Object... params) throws SQLException {
Connection conn = this.prepareConnection();
return this.update(conn, true, sql, params);
}
Wherein the transmission parameter in such a wording: Object ... params Baidu then check the check.
This is the new syntax of JDK1.5, new features, dynamic parameter or variable parameters mean.
(1) ... to declare parameters using a variable length parameters.
(2) variable length parameter must be the last parameter.
(3) Variable parameters can be simultaneously mixed with fixed parameters, but the parameters of a method can not have two types of variable parameters.
Second, how to use
private static int sumUp(int... values) {
int sum = 0;
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
Reference links:
[java method on Object ... args parameter of the meaning]: https: //blog.csdn.net/gao_chun/article/details/42294061
[java variable-length parameters Object ... object]: https: // blog. csdn.net/lcczzu/article/details/46652565
[java understanding of Object ... args]: https: //blog.csdn.net/baidu_28236027/article/details/52471094
Sometimes, we passed to the method parameters number is not fixed, in order to solve this problem, we generally use the following methods: |
I did not find the variable parameters have any much use, almost less than in the framework of the development of J2EE, but sometimes we're not sure when the parameters in the end there'd be this way instead of the past, when passing an array of several the way.
While the variable parameters can be mixed with fixed parameters.
But there are some limitations variable parameters: a method parameter can not have two types of variable parameters, ie:
public void foo (str ... String, Integer ... i) This is not working