print
Advertisment
Advertisment

Var arg method

The ver-arg method is similar to a normal method, with the only difference being that in the ver-arg method, we can pass as many parameter variables as we want. It was interduced in the 1.5 version of Java. Earlier, if we had to pass many parameters to the method, then we had to create separate methods for that. Like if we have a method which has 2 parameters and we have to add them then we can easily do the same if we want to add 50 values then we had to make a parameterise method of 50 variables which was a big failure. Which used to reduce the redibel of the code, now we have to pass only one parameter and it works, which we will see in the example below.

  • We can call or invoke this method by passing any no. Of int values including zero number also.
  • It was interduced in the Java 1.5 version.
  • we can achieve this by using 3(...) dots in method signature.

Example 1

		
	class Test{
		public void add(int a, int b){
			System.out.println(a+b);
		}
		public static void main(String args []){
			Test t = new Test();
			t.add(5,6);
		}
	} 
		
	

output

		11
	

in this example we have create a method for adding 2 values only which is working properly


Example 2

		
	class Test{
		public void add(int a, int b){
			System.out.println(a+b);
		}

		public void add(int a, int b, int c, int d){
			System.out.println(a+b+c+d);
		}
		public static void main(String args []){
			Test t = new Test();
			t.add(5,6);
			t.add(3,6,7,9);
		}
	} 
		
	

output

		
		11
		25
		
	

if we want to add 4 values by using parameter in method then we have to create a different method for using this functionality which is have in above program


Advertisment

Example 3

		
	class Test{	
		public void add(int ... a){ // I am a var-arg method
			int c = 0;
			for (int b : a) {
				c = c+b;
			}
			System.out.println(c);
		}

		public static void main(String args []){
			Test t = new Test();
			t.add(3,6,7,9);
			t.add(3,6,7,78);
			t.add(3,6,7,78);
		}
	} 
		
	

output

		
		25
		26
		27
		
	

if we want to sum nth number which we don't know then how can we achieve this feature only by using var-arg method which is in above program we can call nth number of parameter and it will work properly


Advertisment

what will happen when if we are using 3 parameterized method and var-arg method at a time ?

if we will use both types of method at a time then that method get the first priority who have 3 parameters if that method not found then var-arg method will run

Example 2

		
	class Test{
		public void add(int a, int b){ //I am a normal method with 2 parameter
			System.out.println(a+b);
		}

		public void add(int a, int b, int c, int d){ // I am a normal method with 4 parameter
			System.out.println(a+b+c+d);
		}
		
		public void add(int ... a){ // I am a var-arg method with any number of parameter
			int c = 0;
			for (int b : a) {
				c = c+b;
			}
			System.out.println(c);
		}

		public static void main(String args []){
			Test t = new Test();
			t.add(3,6,7,9);
			t.add(3,6,7,78);
			t.add(3,6,7,78,4,61,41);
		}
	} 
		
	

output

	
		25
		94
		200
	
	
Advertisment
Advertisment
arrow_upward