Nine java learning: method Object ... args parameter meaning

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:

1. Heavy-duty, heavy-duty multi several methods, as far as possible to meet the number of parameters. Obviously this is not a good idea.

2. The parameters passed as an array. Although such a method we simply can, however, in order to pass this array, we need to declare an array, and then added to the array parameters one by one.

Now, we can use variable-length parameters to solve this problem.

Declare variable-length parameters as follows:

public void myMethod (String arg1, Object ... args)

is the parameter to use ... to declare a variable length parameters.

Obviously, the variable-length argument must be the last parameter.

See Tang Buddhist master to say the following examples Monkey:

Package com.kuaff.jdk5;

public class Varargs1
{
    public void Speak (String name, arguments Object ...)
    {
        for (Object Object: arguments)
        {

            System.out.println (Object);

        }
    }

    public static void main (String [] args)

    {
        VA = new new Varargs1 Varargs1 ();

        va.speak ( "Monkey King", "and goblins are the mother of students,");

        va.speak ( "Monkey King", "Different people are people born of his mother," "demon demon his mother was born");

    }
}
wherein speak parameters is declared a variable length parameter, so to speak method you can pass a plurality of parameters.

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

Published 377 original articles · won praise 145 · views 210 000 +

Guess you like

Origin blog.csdn.net/Windgs_YF/article/details/104293001
Recommended