Java Varargs method in Hindi (Variable Argument)- Core Java in Hindi

Java Varargs method in Hindi (Variable Argument)- Core Java in Hindi

0
261
views
Java Variable Argument- Varargs method in Hindi
Java Variable Argument- Varargs method in Hindi

Variable Argument- Varargs method in Hindi

Varargs method with simple example

Everyone want to know what is Varargs method and how does varargs method– (Variable Argument method) work. In this (varargs method) Variable Argument provide detail description with simple examples.  toUnitil 1.4 version we can’t declare a method with variable no of arguments, if there is any change in no of arguments compulsory we should declare a new method. This approach increase length of the code & reduce readability.

To resolve these problem sun people introduced Varargs method in 1.5 version. Hence from 1.5 version onwards we can declare a method with variable no.of arguments such type of methods are called Varargs method.

You may like to check all Core java tutorial in Hindi

Java Variable Argument- Varargs method in Hindi
Java Variable Argument- Varargs method in Hindi

Varargs method Example

H1(int…  x)

We can invoke this method by passing any no. Of int values including zero no. Also.
Example:-

H1( );
H1(10, 20);
H1(10);
H1(10, 20, 30, 40);

Above all are very right
For your clarity Varargs method simple example-

class go4vidhya
{
public static void H1(int…  i)
{
System.out.println(“varargs method in hindi“);
}
public  static void main(string[ ]  args)
{
H1( );
H1(10);
H1)10,20);
H1(10, 20, 30, 40);
}
}

Internally Java Variable types argument method is implemented by using single dimension array concept. Hence with in the var-arg method we can differentiate arguments by using index.

you may like to check Java Anonymous Array

Example:-
class go4vidhya
{
public static void sum(int…  x)
{
int total=0;
for(int y: x)
{
total =total+y;
}
System.out.println(“variable argument method the sum:” +total);
}
public static void main(String[ ] args)
{
sum();
sum(10, 20);
sum(10, 20, 30);
sum(10, 20, 30, 40);
}
}

Output:

variable argument method the sum: 0

variable argument method the sum: 30
variable argument method the sum: 60
variable argument method the sum: 100

In this variable argument method in hindi, I have provided some cases also which is help you to right things

Case 1:
Which of the following Varargs method declaration are valid.
H1(int…  x); //right
H1(int  x…); //wrong
H1(int  …x); //right
H1(int  …x); //wrong
H1(int  x…); //wrong

Case 2:
We can mix varargs method parameter with normal prameter also
Example H1(int x, String… y) //right

Case 3:
If we are mixing varargs method parameter with general parameter then var-arg method parameter should be last parameter.
Example:  H1(int… x, String y) //wrong

Case 4:

In any varargs method we can take only one varargs parameter.

Example: H1(int… x, String… y) //wrong

Case 5:
class  go4vidhya
{
public static void m1(int I)
{
System.out.println(“variable argument method in hindi“);
}
public static void main(int…  I)
{
System.out.println(“variable argument method in hindi“);
}
public static void.main(String args[ ])
{
M1( );
M1(10);
M1(10, 20);
}
}

In general variable arguments method will get least priority i.e if no other method matched. Then only var-argument method will get chance. This is similar to default case inside switch.

Case 6:
class go4vidhya
{
public static void m1(int[ ]  x)
{
System.out.println(“int[ ]”);
}
public static void m1(int…  x)
{
System.out.println(“int… “);
}
}
Compile time error:- cannot declare both m1(int[ ]) and m1(int…) in test.

Var-arg vs Single Dimension arrays – you may like to check Java Arrays in Hindi

Case 1:

Whenever single dimensional array present we can replace with var-arg parameter.
Example:-
m1(int[ ]  x)     ===>   m1(int…  x)  //right
main(String[ ]  args)  ===>  main(String…  x) //right

Case 2:

Whenever ever var-arg method parameter present we can’t replace will single dimensional array.
m1(int…  x)  ===>   m1(int[ ]  x)  //wrong

hope you like my varargs method in hindi

LEAVE A REPLY

Please enter your comment!
Please enter your name here