Varargs and overloading. : Varargs « Language Basics « Java






Varargs and overloading.

Varargs and overloading.
  
/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/

public class VarArgs3 { 
 
  static void vaTest(int ... v) { 
    System.out.print("vaTest(int ...): " + "Number of args: " + v.length + 
                     " Contents: "); 
 
    for(int x : v) 
      System.out.print(x + " "); 
 
    System.out.println(); 
  } 
 
  static void vaTest(boolean ... v) { 
    System.out.print("vaTest(boolean ...) " + 
                     "Number of args: " + v.length + 
                     " Contents: "); 
 
    for(boolean x : v) 
      System.out.print(x + " "); 
 
    System.out.println(); 
  } 
 
  static void vaTest(String msg, int ... v) { 
    System.out.print("vaTest(String, int ...): " + 
                     msg + v.length + 
                     " Contents: "); 
 
    for(int x : v) 
      System.out.print(x + " "); 
 
    System.out.println(); 
  } 
 
  public static void main(String args[])  
  { 
    vaTest(1, 2, 3);  
    vaTest("Testing: ", 10, 20); 
    vaTest(true, false, false); 
  } 
}


           
         
    
  








Related examples in the same category

1.VarArgs Example
2.Create a method that accept varargs in Java
3.Varargs, overloading, and ambiguity.
4.Old style: Use an array to pass a variable number of argumentsOld style: Use an array to pass a variable number of arguments
5.Demonstrate variable length arguments. Demonstrate variable length arguments.
6.Use varargs with standard arguments. Use varargs with standard arguments.
7.Java Varargs TesterJava Varargs Tester
8.Java enum and varargs
9.Java varargs: Iterating Over Variable Length Argument Lists
10.New parameter for main method
11.Convert varargs to an array