
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rules for Using Varargs in Java
Since JSE1.5 you can pass a variable number of values as argument to a method. These arguments are known as var args and they are represented by three dots (…)
Syntax
public myMethod(int ... a) { // method body }
Rules to follow while using varargs in Java
- We can have only one variable argument per method. If you try to use more than one variable arguments a compile time error is generated.
Example
In the following Java example we are trying to accepts two varargs from the method sample().
public class VarargsExample{ void demoMethod(int... ages), String... names) { for (int arg: ages) { System.out.println(arg); } } }
Compile time error
On compiling, the above program generates the following error(s) −
VarargsExample.java:2: error: ')' expected void demoMethod(int... ages, String... names) { ^ VarargsExample.java:2: error: <identifier> expected void demoMethod(int... ages, String... names) { ^ VarargsExample.java:2: error: <identifier> expected void demoMethod(int... ages, String... names) { ^ 3 errors
- In the list of arguments of a method varargs must be the last one. Else a compile time error will be generated.
Example
In the following example, the VarargsExample class has a method with name demoMethod() which accepts 3 arguments: a vararg, a String and an integer.
Here we are making the varargs the 1st in the arguments list.
public class VarargsExample{ void demoMethod(int... marks, String name, int age) { System.out.println(); System.out.println("Name: "+name); System.out.println("Age: "+age); System.out.print("Marks: "); for (int m: marks) { System.out.print(m+" "); } } }
Compile time error
On compiling, the above program generates the following error.
VarargsExample.java:2: error: ')' expected void demoMethod(int... marks, String name, int age) { ^ VarargsExample.java:2: error: <identifier> expected void demoMethod(int... marks, String name, int age) { ^ VarargsExample.java:2: error: <identifier> expected void demoMethod(int... marks, String name, int age) { ^ 3 errors
If you compile the same in eclipse it generates an (same) error suggesting that the variable argument should be the last in the list as shown below −
If you want to make this program work you need to place the variable argument at the end of the arguments list of the method.
public class VarargsExample{ void demoMethod(String name, int age, int... marks) { System.out.println(); System.out.println("Name: "+name); System.out.println("Age: "+age); System.out.print("Marks: "); for (int m: marks) { System.out.print(m+" "); } } public static void main(String args[] ){ VarargsExample obj = new VarargsExample(); obj.demoMethod("Krishna", 23, 90, 95, 80, 69 ); obj.demoMethod("Vishnu", 22, 91, 75, 94 ); obj.demoMethod("Kasyap", 25, 85, 82); obj.demoMethod("Vani", 25, 93); } }
Output
Name: Krishna Age: 23 Marks: 90 95 80 69 Name: Vishnu Age: 22 Marks: 91 75 94 Name: Kasyap Age: 25 Marks: 85 82 Name: Vani Age: 25 Marks: 93