Java Variable Argument Lists

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85244997

case 1: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/VarArgs.java

// housekeeping/VarArgs.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using array syntax to create variable argument lists

class A {}

public class VarArgs {
  static void printArray(Object[] args) {
    for (Object obj : args) System.out.print(obj + " ");
    System.out.println();
  }

  public static void main(String[] args) {
    printArray(new Object[] {47, (float) 3.14, 11.11});
    printArray(new Object[] {"one", "two", "three"});
    printArray(new Object[] {new A(), new A(), new A()});
  }
}
/* My Output:
47 3.14 11.11
one two three
A@6d06d69c A@7852e922 A@4e25154f
*/

printArray() takes an array of Object , then steps through the array using the for-in syntax and prints each one. The standard Java library classes produce sensible output, but the objects of the classes created here print the class name, followed by an @ sign and hexadecimal digits. Thus, the default behavior (if you don’t define a toString() method for your class) is to print the class name and the address of the object.

case 2: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/NewVarArgs.java

// housekeeping/NewVarArgs.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using array syntax to create variable argument lists

public class NewVarArgs {
  static void printArray(Object... args) {
    for (Object obj : args) System.out.print(obj + " ");
    System.out.println();
  }

  public static void main(String[] args) {
    // Can take individual elements:
    printArray(47, (float) 3.14, 11.11);
    printArray(47, 3.14F, 11.11);
    printArray("one", "two", "three");
    printArray(new A(), new A(), new A());
    // Or an array:
    printArray((Object[]) new Integer[] {1, 2, 3, 4});
    printArray(); // Empty list is OK
  }
}
/* Output:
47 3.14 11.11
47 3.14 11.11
one two three
A@15db9742 A@6d06d69c A@7852e922
1 2 3 4
*/

Notice the second-to-last line in the program, where an array of Integer (created using autoboxing) is cast to an Object array (to remove a compiler warning) and passed to printArray() . Clearly, the compiler sees this is already an array and performs no conversion on it. The last line of the program shows it’s possible to pass zero arguments to a vararg list.

case 3: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/OptionalTrailingArguments.java

case 4: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/VarargType.java

// housekeeping/VarargType.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class VarargType {
  static void f(Character... args) {
    System.out.print(args.getClass());
    System.out.println(" length " + args.length);
  }

  static void g(int... args) {
    System.out.print(args.getClass());
    System.out.println(" length " + args.length);
  }

  public static void main(String[] args) {
    f('a');
    f();
    g(1);
    g();
    System.out.println("int[]: " + new int[0].getClass());
  }
}
/* Output:
class [Ljava.lang.Character; length 1
class [Ljava.lang.Character; length 0
class [I length 1
class [I length 0
int[]: class [I
*/

The leading [ indicates this is an array of the type that follows. The I is for a primitive int ;  author created an array of int in the last line and printed its type. This verifies that using varargs does not depend on autoboxing, but it actually uses the primitive types. Varargs work harmoniously with autoboxing:

case 5: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/AutoboxingVarargs.java

overload varargs

case 6: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/OverloadingVarargs.java

// housekeeping/OverloadingVarargs.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class OverloadingVarargs {
  static void f(Character... args) {
    System.out.print("first");
    for (Character c : args) System.out.print(" " + c);
    System.out.println();
  }

  static void f(Integer... args) {
    System.out.print("second");
    for (Integer i : args) System.out.print(" " + i);
    System.out.println();
  }

  static void f(Long... args) {
    System.out.println("third");
  }

  public static void main(String[] args) {
    f('a', 'b', 'c');
    f(1);
    f(2, 1);
    f(0);
    f(0L); // good test
    // - f(); // Won't compile -- ambiguous
  }
}
/* Output:
first a b c
second 1
second 2 1
second 0
third
*/

case 7: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/OverloadingVarargs2.java

// housekeeping/OverloadingVarargs2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {WillNotCompile}

public class OverloadingVarargs2 {
  static void f(float i, Character... args) {
    System.out.println("first");
  }

  static void f(Character... args) {
    System.out.print("second");
  }

  public static void main(String[] args) {
    f(1, 'a');
    f('a', 'b'); // compile error
  }
}

My error prompt:

OverloadingVarargs2.java:16: error: reference to f is ambiguous
    f('a', 'b');
    ^
  both method f(float,Character...) in OverloadingVarargs2 and method f(Character...) in OverloadingVarargs2 match
1 error

If you give both methods a non-vararg argument, it works:

case 8: https://github.com/wangbingfeng/OnJava8-Examples/blob/master/housekeeping/OverloadingVarargs3.java

// housekeeping/OverloadingVarargs3.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class OverloadingVarargs3 {
  static void f(float i, Character... args) {
    System.out.println("first");
  }

  static void f(char c, Character... args) {
    System.out.println("second");
    for (Character c1 : args) {
      System.out.print(c1 + " ");
    }
    System.out.println(args);
  }

  public static void main(String[] args) {
    f(1, 'a');
    f('a', 'b', 'c');
  }
}
/* My Output:
first
second
b c [Ljava.lang.Character;@6d06d69c
*/

As a rule of thumb, only use a variable argument list on one version of an overloaded method. Or consider not doing it at all.

references:

1. On Java 8 - Bruce Eckel

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85244997