Var-arg methods (Variable number of arguments methods)

 Until the 1.4 version, we cant declared a method with a variable number of arguments if there is a change in the number of arguments composure we should go for a new method it increases the length of the code and reduces readability to overcome this problem SUN people introduces var-args methods in 1.5 version. according to this, we can declare a method that can take a variable number of arguments such type of methods is called the var-arg methods.

We can declare methods like

sum(int... a){

}

We can call this method by passing any number of int values including the 0 number.

sum(10);

sum(10,20);

sum(10,20,30)

sum(10,20,30,40);

Internally var-arg parameters will be converted into a one-dimensional array hence within the var-arg method by using an index.

if we mixed normal parameter with var-arg parameter then var-arg parameter last parameter

m1(int a,int... y)

Comments

Popular posts from this blog

Primitive Data Types in java

let - Keyword in JavaScript (Block scope)

Coding Problem 4: Smallest window containing 0, 1 and 2 [GFG - Problem Solving]