Var arg method

Example of Var Arg Method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
package MethodOverloading;
 
public class Varmethod
{
   void add(int...args)
   {
     int sum=0;
     for(int i=0;i<args.length;i++)
      {
        sum=sum+args[i];
      }
 
     System.out.println(sum);
   }
 
    void add(int a)
     {
       System.out.println("runnning int arg");
     }
     void add(int a,int b)
     {
       System.out.println("multi arg method");
     }
     public static void main(String[] args)
      {
         Varmethod v=new Varmethod();
          v.add(5);// int arg method called
          v.add(5,7);//two int arg method called
          v.add(2,2,3,4);// var arg method called
   }
}
 
OUTPUT:
runnning int arg
multi arg method
11

Leave a Reply

Your email address will not be published. Required fields are marked *