Variable parameter list-java SE5 new feature (GO)

Source: Internet
Author: User

The Java1.5 adds new features:
Variable parameters:
For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array.
Note: The mutable parameter must be in the last item .
When there is more than one variable parameter, there must be one that is not the last, so only one variable parameter is supported .
Because of the variable number of arguments, Java cannot differentiate whether the passed parameter belongs to the previous variable parameter or the back parameter, so only the variable parameter can be placed in the last item, when there is the same type parameter behind it.

Features of variable parameters:
(1) can only appear at the end of the parameter list;
(2) ... Between the variable type and the variable name, there are no spaces before and after it can be;
(3) When a method of a variable parameter is called, the compiler creates an array for the mutable parameter implicitly, accessing the mutable parameter in the form of an array in the method body.


1. A simple implementation of a mutable parameter list
When a method is called, the number of arguments or the type of the method is unknown, which is called a mutable argument list. In previous Java code, you could use an object array to implement such a function. Because all classes are directly or indirectly inherited from the object class.

Varargs.java
Package sample;

Class a1{}

public class VarArgs {
static void PrintArray (object[] args) {
for (Object Obj:args)
System.out.print (obj+ "");
System.out.println ();
}

public static void Main (string[] args) {
PrintArray (New object[]{
New Integer, New Float (3.14), New Double (11.11)
});
PrintArray (New object[]{"One", "one", "three"});
PrintArray (New Object[]{new A1 (), New A1 (), New A1 ()});
}
}

Results:
47 3.14 11.11
One and three
[email protected] [email protected] [email protected]

Here the PrintArray method uses the object array as an argument and uses the foreach syntax to iterate through the array and print each object.

2. Java SE5 Implementing a mutable parameter list
In the same way, parameters can be defined as such, (Object...args), so that the same effect is achieved.

Newvarargs.java
Package sample;

Class a{}

public class Newvarargs {
static void PrintArray (Object...args) {
for (Object Obj:args)
System.out.print (obj+ "");
System.out.println ();
}

public static void Main (string[] args) {
PrintArray (New Integer, New Float (3.14), New Double (11.11));
PrintArray (47,
PrintArray ("One", "one", "one", "three");
PrintArray (New A (), new A (), new A ());
PrintArray ((object[]) new integer[]{1,2,3,4});
PrintArray ();
}
}

Results:
47 3.14 11.11
47 3.14 11.11
One and three
[email protected] [email protected] [email protected]
1 2 3 4

There is no explicit use of an array as an argument, but the compiler will actually populate the array for you. So you can also use the foreach syntax to traverse it.
Note the second-to-last line, the compiler has found it to be an array, so no transformations are performed on it. The last line indicates that 0 parameters can be passed.
You can also specify the type of all mutable parameters, and the following program specifies that all mutable parameters must be string.

Optionalarguments.java
Package sample;

public class Optionalarguments {
static void F (string...trailing) {
for (String s:trailing)
System.out.print (s+ "");
System.out.println ();
}

public static void Main (string[] args) {
F ("one");
F ("A", "three");
f ();
}
}

Results:
One
Three

You can use both the original type and the wrapper class in a mutable argument list.

Autoboxingvarargs.java
Package sample;

public class Autoboxingvarargs {
public static void F (Integer...args) {
for (Integer I:args)
System.out.print (i+ "");
System.out.println ();
}

public static void Main (string[] args) {
F (New Integer (1), new Integer (2));
f (3,4,5,6,7,8,9);
F (10,new Integer (11), 12);
}
}

Results:
1 2
3 4 5 6 7 8 9
10 11 12

3. Overloads of the mutable parameter list (overloading)
If an overload occurs, the compiler automatically calls the most appropriate method to match it.

Overloadingvarargs.java
Package sample;

public class Overloadingvarargs {
static void F (Character...args) {
System.out.print ("first");
for (Character C:args)
System.out.print (c+ "");
System.out.println ();
}

static void F (Integer...args) {
System.out.print ("second");
for (Integer I:args)
System.out.print (i+ "");
System.out.println ();
}

static void F (Long...args) {
System.out.print ("third");
for (Long L:args)
System.out.print (L + "");
System.out.println ();
}

static void F (Double...args) {
System.out.print ("forth");
for (Double D:args)
System.out.print (d+ "");
System.out.println ();
}

public static void Main (string[] args) {
F (' A ', ' B ', ' C ');
f (1);
f (2,1);
f (0.1);
F
}
}

Results:
First A B C
Second 1
Second 2 1
Forth 0.1
Third 0

However, the following scenario is not allowed, that is, adding a non-mutable parameter to a method.

Overloadingvarargs2.java
Package sample;

public class OverloadingVarargs2 {
static void F (float I,character...args) {
System.out.println ("first");
}

static void F (Character...args) {
System.out.println ("second");
}

public static void Main (string[] args) {
F (1, ' a ');
F (' A ', ' B ');//error
}
}

But you can solve the problem this way.
Package sample;

public class OverloadingVarargs2 {
static void F (float I,character...args) {
System.out.println ("first");
}

static void F (char C,character...args) {
System.out.println ("second");
}

public static void Main (string[] args) {
F (1, ' a ');
F (' A ', ' B ');
}
}
That is, overloaded methods must maintain a consistent parameter form.

Http://www.blogjava.net/Carter0618/archive/2007/08/19/137889.html

Variable parameter list-java SE5 new feature (GO)

Related Article

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.