- 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.
- void method(int... i){
- }
- Internally Var-arg is a single dimensional array. So we can also pass single dimensional array as argument.
- package instanceofjavaforus;
- public class VDemo{
- void print(int... args){
- for(int i=0;i<args.length;i++){
- System.out.println(args[i]);
- }
- System.out.println();
- }
- public static void main(String args[]){
- Vdemo obj= new Vdemo();
- int x={1,2};
- obj.print(x);// works fine
- obj.print(1,2,3,4);// works fine
- obj.print();// works fine
- //int... var=new int[5]; compile time error: not a valid statement
- }
- Output:
- 1
- 2
- 1
- 2
- 3
- 4
It must be last argument:
- package instanceofjavaforus;
- public class VDemo{
- void print(int x){
- System.out.println("int x");
- }
- void print(int x, int y){
- System.out.println("int x, int y");
- }
- void print(int x, int y,int... args){
- System.out.println("int x, int y,int... args");
- }
- void print(int... args){
- System.out.println("int x, int y");
- }
- public static void main(String args[]){
- Vdemo obj= new Vdemo();
- int x={1,2};
- obj.print(1);
- obj.print(x);// works fine
- obj.print(1,2,3,4);// works fine
- obj.print();// works fine
- //int... var=new int[5]; compile time error: not a valid statement
- }
- Output:
- int x
- int x, int y
- 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.
- void print(int x, int y, int...args, int z){// compile time error
- // throws Compile time error:The variable argument type int of the method print must be the
- // last parameter
- System.out.println("int x, int y");
- }
- void print(int x, int y,int... args){ // valid
- System.out.println("int x, int y,int... args");
- }
- package instanceofjavaforus;
- public class VDemo{
- void print(int... args){
- System.out.println("var arg method");
- }
- void print(int args){
- System.out.println("normal method");
- }
- public static void main(String args[]){
- Vdemo obj= new Vdemo();
- obj.print(1);// works fine
- }
- }
- Output:
- normal method
Overloading method with normal and varargs types in super and sub classes:
var args Program 1:
- package instanceofjavaforus;
- public class VDemo{
- void print(int... args){
- System.out.println("super class method");
- }
- }
- package instanceofjavaforus;
- public class Test extends VDemo{
- void print(int args){
- System.out.println("sub class method");
- }
- public static void main(String args[]){
- VDemo vobj= new VDemo();
- vobj.print(10);
- Test tobj= new Test();
- tobj.print(10);
- }
- }
- OutPut:
- Super class method
- Sub class method
var args Program 2:
- package instanceofjavaforus;
- public class VDemo{
- void print(int args){
- System.out.println("super class method");
- }
- }
- package instanceofjavaforus;
- public class Test extends VDemo{
- void print(int... args){
- System.out.println("sub class method");
- }
- public static void main(String args[]){
- VDemo vobj= new VDemo();
- vobj.print(10);
- Test tobj= new Test();
- tobj.print(10);
- }
- }
- OutPut:
- Super class method
- Super class method
X is smaller than 12 and equals or larger than 10
ReplyDelete