var-arg method (variable number of argument method )
- Until 1.4 we can't declare a method with the variable number of arguments.If there is a change in the number of arguments compulsory we should go for the new method.But this increases the length of the code & readability.
- To overcome this problem java makers introduced the var-args method in 1.5 version of JDK(java).According to this, we can declare a method which can take the variable number of arguments, such type of method are called var-arg methods.
- we can declare a var-arg method as follows:-
Syntax:
m1(int... x)
where m1 is a method name & int .... x as an arguments type (we can take any type)
4.we can call this method by passing any number of arguments of int values including 0 number also.
example 1:-
m1( );
m1(10);
m1(10,20 );
m1(10,20,30,40, );
example 2:-
class Test
{
public static void m1()
{
System.out.println("var-arg method called");
}
public static void main(System.out.println);
{
m1();
m1(10);
m1(10,20);
m1(10,20,30,40);
}
}
o/p of the above program is as follows;-
var-arg method called
var-arg method called
var-arg method called
var-arg method called
var-arg method called
var-arg method called
critical point while using the var-arg method
1. Below them let's check which is the correct use of the var- arg method.
example:
- m1(int ... x) (valid)
- m1(int ...x ) (valid)
- m1(int...x ) (valid)
- m1(int x... ) (invalid)
- m1(int. ..x ) (invalid)
- m1(int .x.. ) (invalid)
we should not separate the dots .
2. We can use mix var-arg parameter with normal arguments.
example:-
m1(int x,int... y) (valid)
m1(String s,double... y) (valid)
3.If we are using normal parameters with var-arg parameter the var-arg parameter should be the last parameter.
example:-
m1(String ....s ,double d) (invalid)
m1(double d, String... s) (valid)
4. Inside the var-arg method, we can take only one var-arg parameter and we can't take more than one var-arg parameter.
example:-
m1(String ....s ,double d) (invalid)
5.If there is a collision between var-arg method & general method then general method always wins.
example:-
class Test
{
public static void m1(int... x)
{
System.out.println("var-arg method called");
}
public static void m1(int x)
{
System.out.println("general method called");
}
public static void main(String [ ] args )
{
m1();
m1(10,20);
m1(10,20,30,40);
m1(10);
}
}
The output of the following program is:-
var-arg method called
var-arg method called
var-arg method called
general method called
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home