» » VarArgs in java

  • The full form of var-arg is variable length arguments.
  • Till JDK 1.4 we must define overloaded methods to pass different list of arguments .
  • But using this feature we can define method to pass ZERO to 'n'  number of arguments.
  • Syntax: After data type we must place three dots.


  1.          void method(int... i){
  2.          }
  • Internally Var-arg is a single dimensional array. So we can also pass single dimensional array as argument.
  1. package instanceofjavaforus;
  2. public class VDemo{ 
  3.  void print(int... args){
  4. for(int i=0;i<args.length;i++){
  5. System.out.println(args[i]);
  6. }
  7. System.out.println();
  8. }
  9. public static void main(String args[]){
  10.  Vdemo obj= new Vdemo();
  11. int x={1,2};
  12. obj.print(x);// works fine
  13. obj.print(1,2,3,4);// works fine
  14. obj.print();// works fine
  15. //int... var=new int[5]; compile time error: not a valid statement
  16.  
  17. Output:
  18. 1
  19. 2

  20. 1
  21. 2
  22. 3
  23. 4

It must be last argument:

  1. package instanceofjavaforus;
  2. public class VDemo{ 
  3. void print(int x){
  4.  System.out.println("int x");
  5. }
  6.  
  7. void print(int x, int y){
  8.  System.out.println("int x, int y");
  9.  void print(int x, int y,int... args){
  10. System.out.println("int x, int y,int... args");
  11. }
  12. void print(int... args){
  13. System.out.println("int x, int y");
  14. }
  15.  public static void main(String args[]){
  16.  Vdemo obj= new Vdemo();
  17. int x={1,2};
  18. obj.print(1);
  19. obj.print(x);// works fine
  20. obj.print(1,2,3,4);// works fine
  21. obj.print();// works fine
  22. //int... var=new int[5]; compile time error: not a valid statement

  23. Output:
  24. int x
  25. int x, int y
  26. int x, int y,int... args
  • if we declared  placed any type after var-arg in the arguments list of a method then it throws a compile time error.

  1. void print(int x, int y, int...args, int z){// compile time error
  2. // throws Compile time error:The variable argument type int of the method print must be the
  3.  // last parameter
  4.  System.out.println("int x, int y");
  5.  void print(int x, int y,int... args){  // valid
  6. System.out.println("int x, int y,int... args");
  7. }
Overloading method with normal and varargs types :

  1. package instanceofjavaforus;
  2. public class VDemo{ 
  3.  void print(int... args){
  4. System.out.println("var arg method");
  5. }
  6. void print(int args){
  7. System.out.println("normal method");
  8. }
  9.  
  10. public static void main(String args[]){
  11.  Vdemo obj= new Vdemo();
  12. obj.print(1);// works fine
  13.  }
  14. Output:
  15. normal method

Overloading method with normal and varargs types in super and sub classes:

var args Program 1:

  1. package instanceofjavaforus;
  2. public class VDemo{ 
  3.  void print(int... args){
  4. System.out.println("super class method");
  5. }

  1. package instanceofjavaforus;
  2. public class Test extends VDemo{ 
  3.  void print(int args){
  4. System.out.println("sub class method");
  5. }
  6. public static void main(String args[]){
  7.  VDemo vobj= new VDemo();
  8. vobj.print(10);
  9. Test tobj= new Test();
  10. tobj.print(10);
  11. }
  12. }
  13. OutPut:
  14. Super class method
  15. Sub class method

var args Program 2:

  1. package instanceofjavaforus;
  2. public class VDemo{ 
  3.  void print(int args){
  4. System.out.println("super class method");
  5. }
  6. }



  1. package instanceofjavaforus;
  2.  
  3. public class Test extends VDemo{ 
  4.  
  5.  void print(int... args){
  6. System.out.println("sub class method");
  7. }
  8. public static void main(String args[]){
  9.  
  10.  VDemo vobj= new VDemo();
  11. vobj.print(10);
  12. Test tobj= new Test();
  13. tobj.print(10);
  14.  
  15. }
  16. }
  17. OutPut:
  18. Super class method
  19. Super class method

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for VarArgs in java

Select Menu