Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dots
operator
java

What are the three dots in Java?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring