Different type of Var-arg rules in Java | Geek On Java - Hub for Java and Android

728x90 AdSpace

Latest News
Sunday, 2 August 2015

Different type of Var-arg rules in Java

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.

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.
Do you know : Number Is Automorphic Or Not in Java

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.
Example:
package com.blogspot.geekonjava.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 

Q: Why var-args should be the last parameter in the list?

A: 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.

Easy step for :  CRUD with Mysql in Java

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"

  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Different type of Var-arg rules in Java Rating: 5 Reviewed By: Geek On Java