Implementation of the Java variable parameter function

Source: Internet
Author: User

The implementation of the variable parameter function in Java is actually an array, and its simple usage is as follows

public class Variableparamtest {
private static void Variableparam (Object ... args) {
for (Object V:args) {
System.out.print (v.tostring () + "");
}
System.out.println ();
}

private static void VariableParam2 (int a, int ... args) {
System.out.println ("A:" + a);
System.out.print ("Variable params num:" + args.length);
}

public static void Main (string[] args) {
Variableparam (1, 2.0F, 3L, ' Z ');
VariableParam2 (' A ', 2, 3, 4);
}
}

1 2.0 3 Z
A:65
Variable params num:3

The Java-based root class is object, and we can define the variable function type as object so that all different types of variable-length parameters can be supported, and the Java arguments provide automatic wrapper functionality, such as Int->integer, Float->float, and so on.

In addition, we can combine the arguments with the non-variable parameters, such as variableParam2 () in the previous example, and the value of the argument function is allowed to be null.

Implementation of the Java variable parameter function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.