Friday, January 20, 2012

Varargs Java 1.5


Varargs:

Varargs is  introduced from Java 1.5. by using varargs  Programmers can create methods that receive an unspecified number of arguments.

In method varargs can be declare as
                      
                                Data_type… variable_name


Example:
                         public void sum(int... a){
                       
                          }                

public void sum(int... a) indicates that the method receives a variable number of arguments of integer type.


Example:


package com.jsl.core;
public class VarArgsDemo {
           
                public int  sum(int... a){
                  
                        int res=0;
                      
                        for(int i=0;i<a.length;i++){
                                    res=res+a[i];
                        }
                      
                         return res;
            }
           
            public static void main(String[] args) {
                       
                        VarArgsDemo obj=new VarArgsDemo();
     
                        System.out.println("The sum of two variable is :"+obj.sum(10,20));
           
                        System.out.println("The sum of three variable is :"+obj.sum(10,20,30));

                         System.out.println("The sum of four variable is :"+obj.sum(10,20,30,40));
                       
            }

}


Output:

The sum of two variable is   :30
The sum of three variable is :60
The sum of four variable is  :100


Example:

package com.jsl.core;
public class VarArgsDemo {
           
public static void main(String... args) {
                                    for(String name:args){
                                                System.out.println(name);
                                    }
            }
}

java VarArgsDemo Jsl Tech Java Training

output
Jsl
Tech
Java
Training

The  varargs length  must be only one variable in method parameter list.

                        public void sum(int... a, float... b) it leads to compilation error,  Because in a method we can declare only one varargs length variable.


If the varargs length variable is combined with other variables, this varargs length variable should be last in the parameter list.

public void sum(int x,float y,int... a)


The method overloading with same type parameter in different length or passing array to a method can accomplished by using varargs.

Example:


package com.jsl.core;
public class VarArgsDemo {
      public int sum(int[]...a){
                  int res=0;
                  for(int i=0;i<a.length;i++){
                        for(int j=0;j<a.length;j++){
                              res+=a[i][j];
                        }
                  }
                  return res;
      }
     
      public static void main(String... args) {
         
     VarArgsDemo obj=new VarArgsDemo();
     
     System.out.println("The sum is : "+obj.sum(new int[][]{{1,2,3},
                        {4,5,6},{7,8,9}}));
      }
}

output :

The sum is : 45

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...