Trusted answers to developer questions

What are the three dots in Java?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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.

svg viewer

Code 1

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 );
}
}

Code 2

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

dots
operator
java
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?