Powered by Blogger.

Different type of Vararg rules in Java

>> Tuesday, November 25, 2014

While writing methods using variable arguments(var-args), we may confuse in different scenarios like where to put var-args in the parameters and how to place different type of arguments.Here we have given some rules with scenarios to understand easily. This will help you in the SCJP examination and as well, java interviews for freshers and experienced guys.
varargs rules JavabynataraJ
Rule 1: Var-arg syntax is allow only for method parameters not for normal variables.
public void add(int...a)
int... a; //compiler time error
Rule 2: Var-arg syntax is allowed only after parameter type and just before parameter name but it is not allowed after parameter name.
Ex:
public void add(int...a){}
public void multiply(long... l){}
public void add(int a...){} //not valid syntax
Rule 3: Since var-arg is a single dimensional array parameter. We were not allowed to overload the method with the same array([]) parameter type, it leads to compile time error.
Ex: 
public void add(int...a){}
public void add(int[] a){} //Duplicate method add(int[]) in type VarArgsCases
Here both arguments types are same.
Rule 4: Var-arg parameter must be last parameter in the list else it leads to compile time error.']' expected. Ex:
void add(int a,float... b){
void add(int a,float b,long... c){} // compile time error
void add(int...a, float b,long c){} //compile time error
Q: How can we call a method with normal and var-arg parameter types for example above add method.
A: We must call that method by passing that normal parameter type arguments. Here you may get doubt like, Why var-args should be the last parameter in the list. The Answer is To prepare array object as shown in the above program.If we declare var-arg or first or second parameter compiler will not have clue how many arguments must be included in the array object.
for Example:
package com.javabynataraj.core;
public class VarArgsCases {
 public static void display(int a,int...b){
  System.out.print("a="+a+" nArray b: ");
  for(int i=0;i<b.length;i++){
   System.out.print(b[i]+" ");
  }
 }
 public static void main(String[] args) {
  //display();         //compile time error
  display(10);         //compiler understands like: display(10,new int[0]);
  display(10,20);      //compiler understands like: display(10,new int[]{20});
  display(10,20,30);   //compiler understands like: display(10,new int[]{20,30});
  display(10,20,30,40);//compiler understands like: display(10,new int[]{20,30,40});
 }
}
Output:
a=10 
Array b: a=10 
Array b: 20 a=10 
Array b: 20 30 a=10 
Array b: 20 30 40 
one more Question here is: Why var-args should be the last parameter in the list?
Ans: To prepare array object as shown in the above program, if we want to declare var-args as first or second parameter compiler will not have clue how many arguments must be included in the array object.
Rule 5: We cannot declare multiple var-arg parameters even though they are different types. Because, compiler does not have clue with how many values first var-arg array object must be created. It leads to compile time error.
public void add(int... a,int...b){}//leads compile time error
public void add(int... a,long...b){} //compile time error
in the above scenario compiler says - "The variable argument type int of the method add must be the last parameter"

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.