forked from bhagat-hrishi/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVar_arg.java
More file actions
59 lines (51 loc) · 1.86 KB
/
Var_arg.java
File metadata and controls
59 lines (51 loc) · 1.86 KB
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
51
52
53
54
55
56
57
58
59
class Var_arg
{
//This introduced in "1.5" version
public static void m1(int ... x)
{
int sum=0;
for(int i:x)
{
sum+=i;
}
System.out.println("Sum of elements: "+sum);
System.out.println("I am var_arg");
}
/**
In general var-arg method will get least priority that is if no other method
matched then only var-arg method will get the chance this is exactly same as
default case inside a switch.
*/
//Original no arg is prefere with var-arg method
public static void m1()
{
System.out.println("I am original no argument");
}
//we can mix general parameter with var-arg parameter but var-arg in this case should presnt at rightmost side
//this is error public static void general_and_var_arg(float ... f , String s)
public static void general_and_var_arg(String s,float ... f)
{
System.out.println("general_and_var_arg");
}
//As internally var-arg method are implemented as 1D Array
// public static void m1(int [] a)//this is error
// {
// }
//We cannot have more than one variable argument method
//Following is "Error"
// static more_than_one_var_arg(int ... x,float ... f)//Error
// {
// System.out.println("I am more than one var-arg methods");
// }
//As var-arg method is internlly uses 1d array then we can declare main method as below
public static void main(String ... args)//use of var-arg
{
m1(90);//with 1 arg
System.out.println("_______________________\n");
m1();//with no arg
System.out.println("_______________________\n");
m1(3,4,4,5,5,6,6);//with 7 arg
System.out.println("_______________________\n");
general_and_var_arg("Hrishi");
}
}