What is ... (3 dots) found in the Java source? Variadic arguments to know from

Trigger

I saw an argument of ... (3 dots) in the Java source.

java:What...What?


    private static void printArgs(String description, Object... args) {
        for (Object object : args) {
            System.out.println(object);
        }
    }

... (three points) represent variadic arguments.

Variadic arguments can specify multiple arguments of the specified type.

Try to specify the argument


        //This is OK
        printArgs("One", "Multiple");
        //This is also OK
        printArgs("Two", "Multiple", "Designation");
        //This is also OK
        printArgs("Three", "Multiple", "Designation", true);
        //This is OK
        printArgs("None");

result


One:Multiple
Two:Multiple designation
Three:Multiple specifications true
None: 

Variadic arguments are like defining an array.

Compiling a variadic method produces an array.

After compilation


    public void methodName(String description, Object[] vars)
The contents of the method.
    }

Variadic arguments can also be arrays.

You can also specify an array as an argument


//This is OK
methodName("Array", new int[]{1,2,3,4,5});
//Even this is OK
methodName("Array and one", new int[]{1,2,3,4,5}, "Multiple");

Variadic arguments have a rule of use.

** Do not overload with an array as an argument ** Reason: It will be the same when compiled

Do not overload with an array as an argument


    public void methodName(String description, Object... vars)
The contents of the method.
    }
    public void methodName(String description, Object[] vars)
The contents of the method.
    }

** Variadic argument must be specified at the end ** Reason: Maybe because you don't know how much is the value specified for the variadic argument

Variadic argument must be specified at the end


//I get a compile error in args, and in Eclipse I get this message:(Rough translation:Write the variadic argument at the end)
// 「The variable argument type Object of the method printArgs must be the last parameter」
    private static void printArgs2(Object... args, String description) {
The contents of the method.
    }

** Only one variable length argument can be specified ** Reason: Maybe you don't know where the first one is, and the first variadic argument isn't specified at the end.

Only one variable length argument can be specified


//I get a compile error in args2, and in Eclipse I get this message:
// 「The variable argument type Object of the method printArgs must be the last parameter」
    private static void printArgs2(String description, String... args2, Object... args) {
The contents of the method.
    }

//This will not result in a compile error
    private static void printArgs2(String description, String[] args2, Object... args) {
The contents of the method.
    }

bonus

How to test private methods with arrays and variadic arguments in JUnit-Qiita

: sunny: Site explaining variadic arguments: sunny:

-Java Variadic Memo (Hishidama's Java VarArgs Memo) -[Java] Precautions when arranging variable-length argument parameters --Qiita

Recommended Posts

What is ... (3 dots) found in the Java source? Variadic arguments to know from
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
What is the main method in Java?
What to do when you want to know the source position where the method is defined in binding.pry
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
The road from JavaScript to Java
Want to know what Ruby n is the power of 2? (Power judgment of 2)
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What to do if Cloud9 is full in the Rails tutorial
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What is a class in Java language (3 /?)
What to do if the prefix c is not bound in JSP
Java reference to understand in the figure
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
[Java] Flow from source code to execution
How to get the date in java
From Java9, the constructor of the class corresponding to primitive types is deprecated.
[Beginner] What is Docker in the first place? Easy-to-understand explanation from the basics!
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Androd: What to do about "The Realm is already in a write transaction in"
What is Pullback doing in The Composable Architecture
What is JSP? ~ Let's know the basics of JSP !! ~
What is the Java Servlet / JSP MVC model?
What is different from the PHP language. [Note]
What is the volatile modifier for Java variables?
What to do when debugging "Source not found"
[Java] Memo on how to write the source
[Java] Something is displayed as "-0.0" in the output
What is Java <>?
What is Java
Get the value from the array and find out what number it is included in
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
The right way to see the tomcat source in eclipse
[Java] How to get the URL of the transition source
[Java] How to omit the private constructor in Lombok
[Java] I want to calculate the difference from the date
How to write Scala from the perspective of Java
Which is better, Kotlin or Java in the future?
[JAVA] What is the difference between interface and abstract? ?? ??
Java classes and instances to understand in the figure
Summary of how to implement default arguments in Java
I tried to implement the Euclidean algorithm in Java
What Java engineers need to prepare for the Java 11 release
What is the difference between Java EE and Jakarta EE?
The story that .java is also built in Unity 2018
I tried to find out what changed in Java 9
In Java, I want to trim multiple specified characters from only the beginning and end.
What you need to do to open a file from the menu in the document-Based App macOS app
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Comments in Java source
Eval Java source from Java
What is Java Encapsulation?
From Java to Ruby !!
What is Java technology?
What is Java API-java
[Java] What is JavaBeans?
[Java] What is ArrayList?