Home > Java > javaTutorial > Java -- Detailed code explanation of variable parameter list

Java -- Detailed code explanation of variable parameter list

php是最好的语言
Release: 2018-08-08 11:10:13
Original
1534 people have browsed it

The following code is a way to implement a variable parameter list.

    public static void printAry(Object[] objs){        for(Object obj:objs){
        System.out.print(obj+" ");
        }
        System.out.println();
    }    public static void main(String[] args) {
        printAry(new Object[]{1,2,3,4,5});
        printAry(new Object[]{"ni","hao"});

    }
Copy after login
Copy after login

Output result:
Java -- Detailed code explanation of variable parameter list

In this way, any type and number of parameters can be passed into the function. However, the above method is a relatively outdated method. After Java SE5 came out, a more convenient way was provided.

The code is as follows:

public static void printAry(Object... objs){        for(Object obj:objs){
        System.out.print(obj+" ");
        }
        System.out.println();
    }    public static void main(String[] args) {
        printAry(1,2,3,4,5);            
        printAry("ni","hao");           //无需显性的创建数组,由编译器自动填充。
        printAry(new Object[]{"ni","hao"}); //也可传入数组。
        printAry();                         //可为空
        printAry(new Integer(1),new Float(2));//可传不同类型的参数
    }
Copy after login
Copy after login

Result:
Java -- Detailed code explanation of variable parameter list

The following code is a way to implement a variable parameter list.

    public static void printAry(Object[] objs){        for(Object obj:objs){
        System.out.print(obj+" ");
        }
        System.out.println();
    }    public static void main(String[] args) {
        printAry(new Object[]{1,2,3,4,5});
        printAry(new Object[]{"ni","hao"});

    }
Copy after login
Copy after login

Output result:
Java -- Detailed code explanation of variable parameter list

In this way, any type and number of parameters can be passed into the function. However, the above method is a relatively outdated method. After Java SE5 came out, a more convenient way was provided.

The code is as follows:

public static void printAry(Object... objs){        for(Object obj:objs){
        System.out.print(obj+" ");
        }
        System.out.println();
    }    public static void main(String[] args) {
        printAry(1,2,3,4,5);            
        printAry("ni","hao");           //无需显性的创建数组,由编译器自动填充。
        printAry(new Object[]{"ni","hao"}); //也可传入数组。
        printAry();                         //可为空
        printAry(new Integer(1),new Float(2));//可传不同类型的参数
    }
Copy after login
Copy after login

Result:
Java -- Detailed code explanation of variable parameter list

Related recommendations:

Detailed explanation of variable length parameter code in Java

Detailed explanation of the example code of variable parameters in java

Analysis of Java's variable-length parameter list and points to note when using it

The above is the detailed content of Java -- Detailed code explanation of variable parameter list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template