-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableArgumentExamples.java
50 lines (43 loc) · 1.32 KB
/
VariableArgumentExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package concept.examples.varargs;
//Variable Arguments allow calling a method with different number of parameters.
//Lets look at a basic example:
public class VariableArgumentExamples {
// int(type) followed ... (three dot's) is syntax of a variable argument.
public int sum(int... numbers) {
// inside the method a variable argument is similar to an array.
// number can be treated as if it is declared as int[] numbers;
int sum = 0;
for (int number : numbers) {
sum += number;
}
return sum;
}
public static void main(String[] args) {
VariableArgumentExamples example = new VariableArgumentExamples();
// 3 Arguments
System.out.println(example.sum(1, 4, 5));// 10
// 4 Arguments
System.out.println(example.sum(1, 4, 5, 20));// 30
// 0 Arguments
System.out.println(example.sum());// 0
}
// Variable Argument should be always the last parameter (or only parameter)
// of a method.
// Below example gives a compilation error
/*
* public int sum(int... numbers, float value) {//COMPILER ERROR }
*/
// Even a class can be used a variable argument. In the example below, bark
// method
// is overloaded with a variable argument method.
class Animal {
void bark() {
System.out.println("Bark");
}
void bark(Animal... animals) {
for (Animal animal : animals) {
animal.bark();
}
}
}
}