Example of java variable parameter "simple Java"

Source: Internet
Author: User
Tags string format

What is a java mutable parameter

A mutable parameter is a feature introduced in Java 1.5 that allows a method to accept any number of arguments.

 Package Simplejava;  Public class Q25 {    publicstaticvoid  print (String ... s) {        for (String a:s)            System.out.println (a);    }      Public Static void Main (string[] args) {        print ("a");        Print ("A", "B");        Print ("A", "B", "C");}    }
How a variable parameter works

When a variable parameter is used, an array is created, its array length is the number of arguments, then the parameters are passed to the array, and the array is passed to the method.

When to use variable parameters

As its name, the variadic mechanism is useful when a method needs to handle any number of arguments; A good example is the String.Format (String format, Object ... args) method in the JDK, The method can receive any number of parameters for string formatting, so it can be implemented using a variable-parameter mechanism.

Link: http://www.programcreek.com/2014/01/java-varargs-examples/

Example of java variable parameter "simple Java"

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.