Wednesday, July 30, 2014

Vararg in Java : A detailed overview.

2:33 PM



Varargs in Java was introduced since java 1.5, vararg reduces the complexity of overloading a method multiple times.This approach also benefits in terms of less code and code re-usability. Less code further benefits in easy code readability. 

Example prior to varargs

A simple problem to add 2 or more numbers requires the add method to be overloaded multiple times.

//OverloadingDemo.java

class OverloadingDemo
{
                //Function to add 2 numbers
                public int add(int x, int y)
                {
                                int add=x+y;
                                return add;
                }
               
                //Function to add 3 numbers
                public int add(int x, int y, int z)
                {
                                int add=x+y+z;
                                return add;
                }

                //Function to add 4 numbers
                public int add(int x, int y, int z, int i)
                {
                                int add=x+y+z+i;
                                return add;
                }

                public static void main(String [] args)
                {
                                //Object creation
                                OverloadingDemo od=new OverloadingDemo();
                               
                                //Invokes the add(int x, int y) method 
                                System.out.println(od.add(10,20));
                               
                                //Invokes the add(int x, int y, int z) method
                                System.out.println(od.add(10,20,30));
                               
                                //Invokes the add(int x, int y, int z, int i) method
                                System.out.println(od.add(10,20,30,40));
                }
               
}

Since, the above example code leads to repetition of code every time the number of variable increases, also repetition of code makes it harder for other programmer to understand the code.

Think of the above problem when, how many numbers to add are not known at code time?

To solve the above problem Java 1.5 has provided support for verarg type variables.

How the updated code of the above problem will look like after vararg.

//OverloadingDemo.java
class OverloadingDemo
{
                public int add(int...y)
                {
                                int add=0;
                                for(int i:y)
                                {
                                                add=add+i;
                                }
                                return add;
                }
                public static void main(String [] args)
                {
                                //Object creation
                                OverloadingDemo od=new OverloadingDemo();                             
                                System.out.println(od.add(10,20,30,40));
                               
                }
               
}

The above code solves the problem of addition when numbers to add are not known. Also it provides code re usability, better and easy to understand code. 

Rules on var-arg.

1. Vararg must be last parameter in method argument list. Else it leads to compile time error : ')' expected.

2. Vararg type parameter must follow the below syntax.

SYNTAX:

datatype ... variable_name

Violation of above syntax leads to compile time error malformed floating point literal.


3. You can only have 1 vararg variable in a method definition. Else it leads to compile time error.

0 comments :

Post a Comment

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy