The number of parameters that can be changed

Programming requires writing methods (functions). Most methods in Java take a fixed number of arguments, and JDK5 provides mutable arguments to make method calls more flexible.

An array of objects is passed as an argument

/** * Prints the contents of the object array *@paramThe args array * /
static void printArray(Object[] args) {
    for (Object obj : args) {
        System.out.print(obj + "");
    }
    System.out.println();
}
Copy the code
  • printArrayTest
private static void printArrayTest(a) {
    printArray(new Object[]{47, (float) 3.14.11.11});
    printArray(new Object[]{"one"."two"."three"});
    // Prints the reference address of the object
    printArray(new Object[]{new A(), new A(), new A()});
}
Copy the code

Output result:

47 3.14 11.11 one, two three com. Tea. Modules. Java8. The extend. A @ 5910 e440 com. Tea. Modules. Java8. The extend. @ 6267 c3bb A com.tea.modules.java8.extend.A@533ddbaCopy the code

Use mutable parameters instead of object arrays

/** * Prints the list of variable arguments *@paramWhen args specifies an argument, the compiler actually populates the array */ for you
static void printObjectArgs(Object... args) {
    for (Object obj: args) {
        System.out.print(obj + "");
    }
    System.out.println();
}
Copy the code
  • printObjectArgsTest
private static void printObjectArgsTest(a){
    // Can take individual elements:
    printObjectArgs(47, (float) 3.14.11.11);
    printObjectArgs(47.3.14 F.11.11);
    printObjectArgs("one"."two"."three");
    printObjectArgs(new A(), new A(), new A());

    // Or an array:
    printObjectArgs((Object[]) new Integer[] {1.2.3.4});
    // Empty list is OK
    printObjectArgs();
}
Copy the code

Output result:

47 47 3.14 11.11 3.14 11.11 one, two three com tea. Modules. Java8. The extend. @ 5910 e440 A com.tea.modules.java8.extend.A@6267c3bb com.tea.modules.java8.extend.A@533ddba 1 2 3 4Copy the code

Variable parameters do not depend on automatic boxing

/** * Packing type as a variable argument, the passed argument will automatically pack *@param args
 */
static void printfCharArgs(Character... args) {
    System.out.print(args.getClass());
    System.out.println(" length " + args.length);
}

/** * Base type as variable argument *@param args
 */
static void printBaseTypeArgs(int. args) {
    System.out.print(args.getClass());
    System.out.println(" length " + args.length);
}

/** * Variable parameter types: 

* Tests whether the class information of the base type can be printed */
private static void varargType (a) { printfCharArgs('a'); printfCharArgs(); printBaseTypeArgs(1); printBaseTypeArgs(); System.out.println("int[]: "+ new int[0].getClass()); int[] array = new int[] {1.2.3}; System.out.println(array.length); for (int i = 0; i < array.length; i++) { System.out.println(i); }}Copy the code

Output result:

class [Ljava.lang.Character; length 1
class [Ljava.lang.Character; length 0
class [I length 1
class [I length 0
int[]: class [I
Copy the code

Primitive types cannot be used when using generic variable parameters

/** * The type parameter of a generic type must be a class type, not a simple type **@param <T>
 * @param values
 */
private static <T> void printGenericObjects(T... values) {
    for (T value : values) {
        System.out.println(value + ""+ value.getClass()); }}public static void main(String[] args) {
    Integer[] array = new Integer[]{1.2.3};
    int[] arrayB = new int[] {1.2.3};
    printGenericObjects(array);
    printGenericObjects(arrayB);
}
Copy the code

An array of underlying data types is treated as an object. An array of objects is used to iterate over an array of objects.