Home  >  Article  >  Web Front-end  >  A brief introduction to the usage of Varargs

A brief introduction to the usage of Varargs

零下一度
零下一度Original
2017-06-14 16:55:151879browse

J2SE 1.5 provides the "Varargs" mechanism. With this mechanism, you can define formal parameters that can match multiple actual parameters. Thus, a variable number of actual parameters can be passed in a simpler way. This article introduces how to use this mechanism, as well as several issues when this mechanism interacts with arrays, generics, and overloading. As of J2SE 1.4, it has been impossible to define a method with a variable number of actual parameters in a Java program - because Java requires that the number and type of actual parameters (Arguments) and formal parameters (Parameters) must match one by one, and the number of formal parameters It is fixed when the method is defined. Although it is possible to provide versions with different numbers of formal parameters for the same method through the overloading mechanism, this still cannot achieve the purpose of allowing the actual parameter quantities to change arbitrarily. However, the semantics of some methods require that they must be able to accept a variable number of actual parameters - for example, the famous main method needs to be able to accept all command line parameters as actual parameters, and the number of command line parameters cannot be determined in advance. Come down. For this problem, traditionally the approach of "using an array to wrap the actual parameters to be passed" is used to deal with it. Use

1. Understanding of Varargs mechanism in Java

A brief introduction to the usage of Varargs

Introduction: J2SE 1.5 provides the "Varargs" mechanism. With this mechanism, you can define formal parameters that can match multiple actual parameters. Thus, a variable number of actual parameters can be passed in a simpler way. This article introduces how to use this mechanism, as well as several issues when this mechanism interacts with arrays, generics, and overloading. Until J2SE 1.4, it has been impossible to define a variable number of actual parameters in a Java program

2. Java instance - using Varargs in overloading methods

A brief introduction to the usage of Varargs

Introduction: The following example demonstrates how to use variadic parameters in overloaded methods:

3. Java Example - Varargs variable parameter usage

A brief introduction to the usage of Varargs

Introduction : "Varargs" means "variable number of arguments". Sometimes also called simply "variable arguments"

The above is the detailed content of A brief introduction to the usage of Varargs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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