0

//what does the trailing dots mean?

static void add(int...numbers) {
    int sum = 0;
    for(int num:numbers)
    {
        if(sum !=0){
            System.out.print("+");
        }
        sum+=num;
        System.out.print(num);
    }
    System.out.println("="+sum);
}
Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
ghaz
  • 17
  • 3

1 Answers1

0

why do I get error if I remove the dots between int and number?

The dots say that this is a "varargs" parameter; i.e. you can have multiple values; see What do 3 dots next to a parameter type mean in Java?

As a consequence, the actual type of numbers in your example is int[] within the method body.

So, when you remove the ..., the type changes to int. And that means that

    for(int num:numbers)

is invalid because an single int is not iterable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216