Varargs method (Variable arguments) in java


In this tutorial we will learn what is Varargs method i.e. Variable arguments in Java with program and examples, When Varargs method (Variable arguments) was introduced in Java,
What is syntax of Varargs method (Variable arguments) in Java,  how Varargs method allows us to write the methods which can take variable number/length of arguments, Important rules to be kept in mind while using Varargs method in Java like There can only be one Varargs in the method and Varargs must always be the last argument in the method, Interesting fact about main method in Java, AutoBoxing beats Var-args in java and Widening beats Var-args in java.


Also read :

What is Varargs method (Variable arguments) in Java >
Varargs method allows us to write the methods which can take variable length of arguments in java.
Variable arguments as the name suggests can take variable i.e any number of argument (may be 0, or 1, or 2, till any number) in java.

When Varargs method (Variable arguments) was introduced in Java >
Varargs method /Variable arguments were introduced in Java 5.

What is syntax of Varargs method (Variable arguments) in Java >

static void myMethod(String... str){}

Three dots dots signifies the varargs, above Varargs method can take any number of string argument (may be 0, or 1, or 2, till any number) in java.

We have read that Varargs method allows us to write the methods which can take variable number/length of arguments. But how? Let's understand it

Program/ Example where varargs method takes 0 or no argument -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(String... str){
   }
  
   public static void main(String[] args) {
          myMethod();
   }
}

Program/ Example where varargs method takes 1 argument -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(String... str){
   }
  
   public static void main(String[] args) {
          myMethod("a");
   }
}

Program/  Example where varargs method takes 2 argument -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(String... str){
   }
  
   public static void main(String[] args) {
          myMethod("a","b");
   }
}

Program/  Example where varargs method takes 5 argument -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(String... str){
   }
  
   public static void main(String[] args) {
          myMethod("a","b","c","d","e");
   }
}

So, Varargs method can take any number of argument (may be 0, or 1, or 2, till any number) in java.

1 - Important rules to be kept in mind while using Varargs method in Java -There can only be one Varargs in the method. Any attempt to add more than one varargs will generate compilation error in java.

static void myMethod(int i, String... str){} //Valid
static void myMethod(int i, String... str, String... str2){} //compilation error
static void myMethod(String... str, String... str2){} //compilation error
static void myMethod(int... i, String... str){} //compilation error
static void myMethod(int i, int... i1){} //Valid

Valid example of using one Varargs method in Java -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(int i, String... str){
   }
  
   public static void main(String[] args) {
          myMethod(1,       //int argument
                       "a","b"); //Varargs argument
   }
}

2 - Important rules to be kept in mind while using Varargs method in Java - Varargs must always be the last argument in the method. Any attempt to add argument after varargs will generate compilation error in java.

Exact Compilation error = The variable argument type int of the method myMethod must be the last parameter.

static void myMethod(String... str, int i){} //compilation error
static void myMethod(int i, String... str){} //Valid

static void myMethod(String... str, String i){} //compilation error
static void myMethod(String i, String... str){} //Valid


static void myMethod(int... i1, int i){} //compilation error
static void myMethod(int i, int... i1){} //Valid

static void myMethod(String s, int... i1, int i){} //compilation error
static void myMethod(String s, int i, int... i1){} //Valid

static void myMethod(int... i1, String s, int i){} //compilation error
static void myMethod(String s, int i, int... i1){} //Valid

static void myMethod(int... i1, String s, int i){} //compilation error
static void myMethod(String s, int i, int... i1){} //Valid

Valid example of using Varargs as last argument in method in Java -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class VarargsExample {
   static void myMethod(int i1, int i2, String... str){
   }
  
   public static void main(String[] args) {
          myMethod(1, 2,       //int argument
                       "a","b"); //Varargs argument
   }
}

Interesting fact about main method in Java >
main method in java by default/generally contains string array as arguments, like this
Example where main method contains string array as arguments -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class MainContainsStringArrayClass {
   public static void main(String[] args) {
   }
}

but main method can be replaced with varargs as well, like this

Example where main method contains varargs as arguments -
package varargs;
/** varargs - Variable arguments in java - Copyright (c) JavaMadeSoEasy.com */
public class MainContainsVarargsClass {
   public static void main(String... args) {
         
   }
}




Now, Let's understand following important concepts where >
  1. AutoBoxing beats Var-args in java
  2. Widening beats Var-args in java

  1. AutoBoxing beats Var-args in java
public class AutoboxingBeatsVarargs {
   static void m(Integer i1, Integer i2) {
          System.out.println("Integer i1, Integer i2");
   }
   static void m(Integer...i) {
          System.out.println("int...i");
   }
   public static void main(String[] args) {
          int i = 3;
          m(i, i);
   }
}
/*OUTPUT
Integer i1, Integer i2
*/
Compiler prefers to do AutoBoxing than invoking Var-args. Hence, AutoBoxing beats Var-args in java.

  1. Widening beats Var-args in java
public class WideningBeatsVarargs {
  
   public static void main(String[] args) {
          int i = 3;
          m(i, i);
   }
   static void m(float f1, float f2) {
          System.out.println("float f1, float f2");
   }
   static void m(int... i) {
          System.out.println("int...i");
   }
  
}
/*OUTPUT
float f1, float f2
*/

Compiler prefers to do Widening than invoking Var-args. Hence, Widening beats invoking Var-args in java.

You can remember above 2 conversions in this way >
Widening > Autoboxing > Var-args
Means Varargs is always the loser in java.
(Note: It’s not any standard convention)

Summary -

So in this tutorial we learned what is Varargs method i.e. Variable arguments in Java with program and examples, When Varargs method (Variable arguments) was introduced in Java,
syntax of Varargs method (Variable arguments) in Java,  how Varargs method allows us to write the methods which can take variable number/length of arguments, Important rules to be kept in mind while using Varargs method in Java like There can only be one Varargs in the method and Varargs must always be the last argument in the method, Interesting fact about main method in Java, AutoBoxing beats Var-args in java and Widening beats Var-args in java.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>



Collection - List, Set and Map all properties in tabular form



No comments:

Post a Comment