Varargs Array Part 2 Tutorial

This tutorial will discuss the rules for the proper usage of varargs. I will also show you some pitfalls that you should be aware of when integrating varargs into your programs.

  • The arguments passed to the method must be of a compatible data type to the varargs data type declaration.
  • When invoking a method, the argument may be passed as an array or as a sequence of arguments.
  • You may have other parameters in the method parameter list, but only one varargs parameter and it must be the last parameter listed.
  • Methods may be overloaded with vararg parameters, but beware of a problematic situation where an empty argument array or no arguments is passed - I'll explain in the example code.
  • Constructors may also be overloaded with vararg parameters, but beware of a problematic situation where an empty argument array or no arguments is passed - I'll explain in the example code.
  • A legal declaration for the main method can include varargs, for example, public static void main(String ... args)


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md VarargsTwo
C:\Java>cd VarargsTwo
C:\Java\VarargsTwo>Notepad VarargsTwo.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class VarargsTwo {
    public static void main(String ... args) { // varargs in main method declaration
        System.out.print("Command-line arguments: ");
        for (String temp : args) {
            System.out.print(temp + " ");
        }
        System.out.println();

        new VarargsConstructor(12, 5, 5876, 1024); // sequence of int arguments
        new VarargsConstructor("Tom", "Jane", "Bill", "Mary", "Johnny"); // sequence of String arguments
        int iArray[] = { 123, 7, -1034, -1200 } ;
        new VarargsConstructor(iArray); // pass an array 
        //new VarargsConstructor(); // no arguments = empty varargs array. Which constructor to use??? Ambiguity!
        //VarargsConstructor vc = new VarargsConstructor();  // What is this really doing???
        System.out.println();
	
        VarargsMethod vm = new VarargsMethod();
        vm.displayArray(-903, 1, 8276, 41, 7778, 0); // sequence of int arguments
        vm.displayArray("Apple", "Orange", "Pear"); // sequence of String arguments
        String sArray[] = { "A", "B", "C" };	
        vm.displayArray(sArray); // pass an array 
        vm.displayArray("Lotto numbers: ", 7, 12, 14, 23, 26, 31 );
        //vm.displayArray(); // no arguments = empty varargs array. Which method to use??? Ambiguity!
        //String ... tArrary = { "A", "B", "C" }; //No - only valid syntax inside of a parameter list.
    }
}

class VarargsConstructor {
    VarargsConstructor(int ... items) {
        System.out.print("\nConstructor int data type varargs array: ");
        for(int i : items) {        
            System.out.print(i + " ");
        }
    }

    VarargsConstructor(String ... items) {
        System.out.print("\nConstructor String data type varargs array: ");
        for(String s : items) {
            System.out.print(s + " ");
        }
    }
}

class VarargsMethod {
    void displayArray(int ... items) {
        System.out.print("\nMethod int data type varargs array: ");
        for(int i : items) {        
            System.out.print(i + " ");
        }
    }

    void displayArray(String ... items) {
        System.out.print("\nMethod String data type varargs array: ");
        for(String s : items) {        
            System.out.print(s + " ");
        }
    }

    void displayArray(String name, int ... numbers) {
        System.out.println();
        System.out.print(name);
        for(int i : numbers) {        
            System.out.print(i + " ");
        }
    }

    //void displayArray(String ... names, int ... numbers) { } // No - only one varargs per parameter list
    //void displayArray(int ... numbers, String name) { } // No - varargs must be last parameter

}

Now switch back to the command prompt (CMD) and type in javac VarargsTwo.java and press Enter.
Now type in java VarargsTwo and press Enter.


C:\Java\VarargsTwo>javac VarargsTwo.java
C:\Java\VarargsTwo>java VarargsTwo One Two Three
Command-line arguments: One Two Three
Constructor int data type varargs array: 12 5 5876 1024
Constructor String data type varargs array: Tom Jane Bill Mary Johnny
Constructor int data type varargs array: 123 7 -1034 -1200

Method int data type varargs array: -903 1 8276 41 7778 0
Method String data type varargs array: Apple Orange Pear
Method String data type varargs array: A B C
Lotto numbers: 7 12 14 23 26 31


Final thoughts

Varargs are a very useful tool, just be sure to remember that a zero-length array is perfectly valid in Java and it will cause ambiguity issues only if you overload your methods or constructors.


Tutorials