Difference between revisions of "Var-Args"

From Suhrid.net Wiki
Jump to navigationJump to search
 
(7 intermediate revisions by the same user not shown)
Line 21: Line 21:
 
}
 
}
  
 +
</syntaxhighlight>
  
 
* The ellipsis can be located anywhere e.g. (int... nums) or (int ...nums) or (int ... nums) is fine.
 
* The ellipsis can be located anywhere e.g. (int... nums) or (int ...nums) or (int ... nums) is fine.
* Essentially the method argument is treated as an array within the method. so nums is an int[] array.
+
* Essentially the method arguments are implicitly converted into an array and passed to the method. so nums is an int[] array.
 +
* Example main() can be legally rewritten as :
  
 +
<syntaxhighlight lang="java5">
 +
 +
 +
public static void main(String... args) {
 +
 +
}
 +
 +
</syntaxhighlight>
 +
 +
* For a var-args method, the method parameters can also be passed as an array argument. In this case, it will be treated as a non var-args call, since the argument and the parameter both match.
 +
* Example:
 +
 +
<syntaxhighlight lang="java5">
 +
 +
public class VarArgs {
 +
 +
public static void main(String[] args) {
 +
            System.out.println(vasum(2, 3, 4));               //Variable arguments invocation
 +
            System.out.println(vasum(new int[] {1, 2, 3});    //Array invocation also OK for var-args
 +
    System.out.println(arrsum(new int[] {2, 3, 4}));  //Only Array invocation is possible
 +
}
 +
 +
public static int vasum(int ... nums) {
 +
int sum = 0;
 +
for(int i : nums) {
 +
sum += i;
 +
}
 +
return sum;
 +
}
 +
 +
public static int arrsum(int[] nums) {
 +
int sum = 0;
 +
for(int i : nums) {
 +
sum += i;
 +
}
 +
return sum;
 +
}
 +
 +
}
 +
 +
</syntaxhighlight>
 +
 +
 +
* The var-args must be the last parameter in the method's signature. Why ? To resolve situations like this :
 +
 +
<syntaxhighlight lang="java5">
 +
 +
sum2(2, 3);
 +
 +
public static void sum2(int a, int... nums) {
 +
}
 +
 +
</syntaxhighlight>
 +
 +
* Only one var-arg can be declared in the method (even if the var-args are of different types)
 +
 +
<syntaxhighlight lang="java5">
 +
 +
public static void sum3( String... strs, int... nums) { //ILLEGAL
 +
}
 +
 +
</syntaxhighlight>
 +
 +
 +
[[Category:OCPJP]]
 +
 +
 +
== Var-Args Overloading ==
 +
 +
* Because arguments to var-arg methods are implicitly passed as arrays, the following will NOT compile, both the foo()'s have the same signature:
 +
 +
<syntaxhighlight lang="java5">
 +
 +
private static void foo(int...ia2) {
 +
 +
}
 +
 +
private static void foo(int[] ia2) {
 +
 +
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 08:53, 13 September 2011

  • Var-args are used to defined methods with variable argument lists.
  • Syntax <type>... <varargname>
  • Example:
public class VarArgs {

	public static void main(String[] args) {
		System.out.println(sum(2, 3, 4));
	}
	
	public static int sum(int... nums) {
		int sum = 0;
		for(int i : nums) {
			sum += i;
		}
		return sum;
	}

}
  • The ellipsis can be located anywhere e.g. (int... nums) or (int ...nums) or (int ... nums) is fine.
  • Essentially the method arguments are implicitly converted into an array and passed to the method. so nums is an int[] array.
  • Example main() can be legally rewritten as :
public static void main(String... args) {

}
  • For a var-args method, the method parameters can also be passed as an array argument. In this case, it will be treated as a non var-args call, since the argument and the parameter both match.
  • Example:
public class VarArgs {

	public static void main(String[] args) {
            System.out.println(vasum(2, 3, 4));	              //Variable arguments invocation
            System.out.println(vasum(new int[] {1, 2, 3});    //Array invocation also OK for var-args
	    System.out.println(arrsum(new int[] {2, 3, 4}));  //Only Array invocation is possible
	}
	
	public static int vasum(int ... nums) {
		int sum = 0;
		for(int i : nums) {
			sum += i;
		}
		return sum;
	}
	
	public static int arrsum(int[] nums) {
		int sum = 0;
		for(int i : nums) {
			sum += i;
		}
		return sum;
	}

}


  • The var-args must be the last parameter in the method's signature. Why ? To resolve situations like this :
sum2(2, 3); 

public static void sum2(int a, int... nums) {
}
  • Only one var-arg can be declared in the method (even if the var-args are of different types)
public static void sum3( String... strs, int... nums) { //ILLEGAL
}


Var-Args Overloading

  • Because arguments to var-arg methods are implicitly passed as arrays, the following will NOT compile, both the foo()'s have the same signature:
private static void foo(int...ia2) {
		
}
	
private static void foo(int[] ia2) {
		
}