The three dots (...
) are used in a function’s declaration as a parameter. These dots allow zero to multiple arguments to be passed when the function is called. The three dots are also known as var args
.
The following code uses two arguments in a function call.
class main { public static int function (int ... a) { int sum = 0; for (int i : a) sum += i; return sum; } public static void main( String args[] ) { int ans = function(1,1); System.out.println( "Result is "+ ans ); } }
The following code uses three arguments in the same function defined above. This is possible because of the three dots parameter.
class main { public static int function (int ... a) { int sum = 0; for (int i : a) sum += i; return sum; } public static void main( String args[] ) { int ans = function(1,1,1); System.out.println( "Result is "+ ans ); } }
RELATED TAGS
View all Courses