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

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
RELATED COURSES

View all Courses

Keep Exploring