JavaIntellectualTricks

var- arg methods

Variable Number of Argument methods:

Until 1.4 version , we cannot declare a method with variable number of arguments. If there is a change in number of arguments, compulsory we should go for new method, which increases length of the code and reduces readability. To overcome this problem SUN people introduced var-args methods in 1.5 version. According to this we can declare a method which can take variable number of arguments, such type of methods are called var- arg methods.
Syntax: methodName(int... x);


  • We can call this method by passing any number of int values including 0 number. i.e.  sum(int... x) then we can call this sum method by sum(); sum(10); sum(10,20); sum(10,20,30); sum(10,20,30,40);

class Test{
public static void sum(int... x){
System.out.println(“var-arg method”);
}
public static void main(String args []){
sum();
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}
>javac Test.java
>java Test
o/p:      var-arg method
    var-arg method
    var-arg method
    var-arg method
    var-arg method

  • Internally var-arg parameter is converted in to one dimensional array. Hence with in the var-arg method we can differentiate values by using index.

class Test{
public static void sum(int... x){
int total=0;
for(int x1: x){
total=total+x1;
}
System.out.println(“Sum is: ”+ total);
}
public static void main(String args []){
sum();
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}
o/p:   Sum is : 0
Sum is : 10
Sum is : 30
Sum is:  60
Sum is:  100

Q. Which of the following are the valid var-arg declarations?
1. m1(int...  x)  (valid)
2. m1(int   ...x) (valid)
3. m1(int...x)    (valid)
4. m1(int  x...) (Invalid)
5. m1(int.  ..x) (Invalid)
6. m1(int  .x..) (Invalid)

  • we can use var-arg parameter with normal parameter i.e

m1(int x, int... y)
m1(String s,double... d)
If we mix normal parameter with var-arg parameter, then var-arg parameter should be last parameter. i.e.
m1(double... d,String s) (Invalid)
m1(char ch,d,String... s) (valid)

  • Inside var-arg method we can take only one var-arg parameter and we can't take more than one var-arg parameter. i.e.

m1(int...  x ,double... d) (Invalid)

  • Inside a class we can't declare var-arg method and corresponding 1-D array method simultaneously, otherwise we ' ll get compile time error.

class Test{
public static void m1(int...  x){
System.out.println(“int...”);
}
public static void m1(int[]  x){
System.out.println(“int[]”);
}
}
C.E.: cannot declare both m1(int []) and m1(int...) in Test

  • In general, var-arg method will get least priority i.e if no other method matched then only var-arg method will get the chance to execute

.It is exactly same as default case inside switch.
class Test{
public static void m1(int...  x){
System.out.println(“var-arg method”);
}
public static void m1(int  x){
System.out.println(“Normal Method”);
}
public static void main(String args []){
m1(); //  var-arg method
m1(10); // Normal Method
m1(10,20); // var-arg method
}
}

Equivalence between var-arg parameter and one dimensional array:

case 1: Where ever one dimensional array is present, we can replace with var-arg parameter.
m1(int[]  x) = = m1(int...  x) (valid)
Example: main(String[] args) = = main(String...  args)

case 2:  Where ever var-arg parameter ispresent, we can't replace with  one dimensional array.
m1(int...  x) = = m1(int[]  x) (invalid)

Note: 
1. m1(int...  x) = =  int[]  x
we can call this method by passing a group of int values and x will become one dimensional array.
2. m1(int[]...  x) = = int[][]  x

  • We can call this method by passing a group of dimensional int arrays and x will become 2 D int array

class Test{
public static void main(String args []){
int[] a={10,20,30};
int[] b={40,50,60};
m1(a,b);
}
public static void m1(int[]...  x){
for(int[]  x1: x){
System.out.println(x1[0]); // 10, 40
}
}
}

No comments:

Post a Comment