Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
class Varargs {
  private static void displayBooleans(boolean... bool) {
    System.out.print("Number of arguments: " + bool.length + ", Contents: ");

    for (boolean b : bool)
      System.out.print("[" + b + "]");
  } 
  private static void displayBooleans(boolean bool1, boolean bool2) {
    System.out.println("Overloaded method invoked");  
  }
  public static void main(String[] args) {
    displayBooleans(true, false);
  }
}

When run, this program outputsoutput:

Code Block
Overloaded method invoked

because the non-variable nonvariable arity definition is more specific and consequently a better fit for the arguments given. However, this complexity is best avoided.

...

Bibliography

[Bloch 2008]

Item 42: , "Use Varargs Judiciously"

[Steinberg 2005]

"Using the Varargs Language Feature"

[Sun 2006]

varargs

 

...