Var args
When you want to define a method to take multiple values of one specific data type, then var args can be used.
Syntax:
<returnType> <methodName> (<dataType> <FixedArg>, <dataType> ...<VarArgs>) { ... }
Note 1: The following syntaxes are valid (Notice the spaces)
show(int...arr)show(int ...arr)
show(int... arr)
Note 2: In case of overloaded methods, below is the priority
1. Exact match.
2. Higher type (i.e automatic widening)
3. Wrapper classes
4. var args.
Example 1:
Limitation by passing multiple variables of the same data type as arrayclass Lab4 { public static void main(String[] args) { Hello h1 = new Hello(); h1.sum(1,2,3,4,5); h1.sum(1,2,3,4,5,6,7,8); } } class Hello { void sum(int arr[]){ int sum=0; for(int a:arr){ sum+=a; } System.out.println(sum); } } /* Compile Time Error: Lab4.java:6: error: method sum in class Hello cannot be applied to given types; h1.sum(1,2,3,4,5); ^ required: int[] found: int,int,int,int,int reason: actual and formal argument lists differ in length Lab4.java:7: error: method sum in class Hello cannot be applied to given types; h1.sum(1,2,3,4,5,6,7,8); ^ required: int[] found: int,int,int,int,int,int,int,int reason: actual and formal argument lists differ in length 2 errors */
Example 2
class Lab5 { public static void main(String[] args) { Hello h1 = new Hello(); h1.sum(1,2,3); h1.sum(1,2,3,4,5); h1.sum(1,2,3,4,5,6,7,8); } } class Hello { void sum(int ...arr){ int sum=0; System.out.println("int... arr"); System.out.println("Length - "+arr.length); for(int a:arr){ sum+=a; } System.out.println(sum); } } /*output: int ...arr Length : 3 Sum :6 int ...arr Length : 5 Sum :15 int ...arr Length : 8 Sum :36 */
Example 3:
There can be only one variable argument in the method. More than 1 variable argument throws C.T.Eclass Lab10 { public static void main(String[] args) { Hello h = new Hello(); h.show(10); } } class Hello { void show(int ...arr, int ...arr1){ System.out.println("\nshow(int..., int...)"); } } /* Lab10.java:12: error: ')' expected void show(int ...arr, int ...arr1){ ^ Lab10.java:12: error: <identifier> expected void show(int ...arr, int ...arr1){ ^ Lab10.java:12: error: <identifier> expected void show(int ...arr, int ...arr1){ ^ 3 errors */
Example 4:
Variable argument (varargs) must be the last argument.class Lab8 { public static void main(String[] args) { Hello h1 = new Hello(); h1.show(10); h1.show(10,20); h1.show(10,20, 30); } } class Hello { void show(int a, int...ab){ System.out.println("int, int...ab"); } } /*output: int, int...ab int, int...ab int, int...ab */
Example 5:
Another example with var args as the last argumentclass Lab12 { public static void main(String[] args) { Hello h = new Hello(); h.show("Sujay", 10); } } class Hello { void show(String str, int ...arr){ System.out.println(str+"\t"+arr); } } /*output: Sujay [I@15db9742 //array address */
Example 6:
Pass 2-D array as a parameterclass Lab15 { public static void main(String[] args) { Hello h = new Hello(); int arr1[] = new int[]{1,2}; int arr2[] = new int[]{4,5}; h.show(arr1, arr2); } } class Hello { void show(int[]... arr){ System.out.println("show int[]..."); for(int[] temp:arr){ for(int a:temp){ System.out.print(a+"\t"); } System.out.println(); } } } /*output: show int[]... 1 2 4 5 */
Example 7:
Note how you specifiy the 2-D array as parametersclass Lab16 { public static void main(String[] args) { new Hello().show((new int[]{1,2,3}), (new int[]{4,5,6})); } } class Hello { void show(int...arr[]){ System.out.println("int ...arr[]"); for(int[] temp:arr){ for(int a:temp){ System.out.print(a+"\t"); } System.out.println(); } } } /* Lab16.java:11: error: legacy array notation not allowed on variable-arity parameter void show(int...arr[]){ ^ 1 error */
Example 8:
Overloaded method with exact match and var args... Exact match has the highest priorityclass Lab19 { public static void main(String[] args) { Hello h = new Hello(); h.show(10,20); } } class Hello { void show(int...b){ System.out.println("show(int...)"); } void show(int a, int b){ System.out.println("show(int a, int b)"); } } /*output: show(int a, int b) */
Example 9:
Overloaded methods... automatic widening (casting) has higher priority over vargsclass Lab20 { public static void main(String[] args) { Hello h = new Hello(); h.show(10,20); } } class Hello { void show(int...b){ System.out.println("show(int...)"); } void show(long a, long b){ System.out.println("show(long a, long b)"); } } /*output: show(long a, long b) */
Example 10:
Ambiguity as the method call matches both the methods...C.T.Eclass Lab21 { public static void main(String[] args) { Hello h = new Hello(); h.show(null); } } class Hello { void show(int...b){ System.out.println("show(int...)"); } void show(String...a){ System.out.println("show(String...)"); } } /* Lab21.java:6: error: reference to show is ambiguous h.show(null); ^ both method show(int...) in Hello and method show(String...) in Hello match 1 error */
Example 11:
null, null cannot be stored in Integer array, Therefore String array method is executed.class Lab22 { public static void main(String[] args) { Hello h = new Hello(); h.show(null, null); } } class Hello { void show(int...b){ System.out.println("show(int...)"); } void show(String...a){ System.out.println("show(String...)"); } } /* show(String...) */
Summary
- To define Var-Args parameter, you need to use ellipsis [three dot(...)] after data type before the variable name.
Ex: void sum(int... values) - Var-Args parameter will be converted to an array by the java compiler while generating the class file.
Ex: void sum(int ...values) ->void sum(int[] values) - When you are calling a method which contains Var-Args parameter by passing zero or more values then Java compiler converts it into an array.
Ex:
sum(); ->sum(new int[0]{})
sum(99, 88); ->sum(new int[99, 88]{})
sum(99, 88, 77); ->sum(new int[99, 88, 77]{})
- Var-args can be used only to define parameters of method or constructor and can't be used for instance variables, static variables or local variables.
- Var-Args parameters is equivalent to an array. So data available in Var-Args parameter can be accessed like array.
Ex.
void sum(int ...values){ for(int i=0;i<values.length;i++){ System.out.println(values[i]); } }
- You can pass actual values or array object to the method which is having Var-Args.
Ex:
sum(10,20); int arr[] = new int[]{10,20}; sum(arr);
No comments:
Post a Comment