Parse Java variable long parameter list and its use point of attention _java

Source: Internet
Author: User

Java variable parameter list

Copy Code code as follows:

Class A {}


Because all classes inherit from object, you can use an object array as a parameter method:

public class parameter {
  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", "two", "there"});
    PrintArray (New Object[]{new A (), new A (), New A ()});
  }

After the Java SE5 version has added attributes that can be written like this:

public class Parameter {public
  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 (3.14F, 11.11);
    PrintArray ("One", "two", "three");
    PrintArray (New A (), new A (), new A ());
    PrintArray ((object[]) new Integer[]{1, 2, 3, 4});
    PrintArray ();
  }

You can use the argument list as object:

public class varargtype{
  static void F (Character. args) {
    System.out.print (Args.getclass ());
    System.out.println ("Length" + args.length);
  }
  
  static void g (int ... args) {System.out.print (
    args.getclass ());
    System.out.println ("Length" + args.length);
  }
  
  public static void Main (string[] args) {
    f (' a ');
    f ();
    g (1);
    g ();
    SYSTEM.OUT.PRINTLN ("int []" + New Int[0].getclass ());
  }

This is an attribute introduced in Java 5, and if a method is going to receive an indeterminate number of parameters, then this feature can be useful.

For example, where IO operations are involved, there is basically a minimum of two streams to be turned off: input, output, and I like to encapsulate the operation of the flow closure into the following method so that you can turn off multiple streams with one call.

public static void Closesilent (Closeable ... closeables) {for
   (closeable closeable:closeables) {
     if (closeabl e!= null) {
        try {
          closeable.close ();
        } catch (IOException ignored) {
        }}}
}

This is the only place I think this feature is suitable for use, with the following features:

These parameters have the same type;
The number of parameters is not certain, each one is optional;
The use of these parameters is the same, for example, the above are performed off.
Java variable-length parameter lists can only be placed at the end of the method parameter list.


implementation of Java variable long parameter list

The implementation of a Java variable-length parameter list is passed through the compiler to encapsulate these parameters into an array.

For example, the signature of the method above is actually: Closesilent (closeable[] closeables) void.

Step on the pit.

There is a way, by a, b two places A, b called, September, here in a need to add a parameter, then the brain a cramp, decided to use variable long parameter list, feel B there is no need to change the simple point, the pit is buried like this.

Recently asked B here to add two parameters, then in the method's parameter list continue to add parameters, the types of these parameters are different, so the variable length argument lists are declared Object type.

The first pit is in this method to take variable length parameters of the elements, do not consider some of the parameters are not transmitted, direct detonation array offside anomaly. Immediately feel variable long parameter list is not good, then it is not necessary to change the conventional fixed form of parameter transfer.

After the change, in the test environment measured is no problem. The production environment after the replacement of several classes, the result is an error, the method can not find, a look at the method signature, or array, has not been replaced. From the source view, that call place does not need to change, so did not expect to replace; Because the test environment is full package, there is no problem.

The signature of the method is determined at compile time, and the source level does not appear to need to be changed does not mean that the compiled class does not need to be replaced.

In fact, I have heard before, in this case, the contract is not standardized, the source of a constant value after the change, only to replace the definition of the constant class file, not all references to this constant class file recompile replacement, resulting in inexplicable problems. The situation with the method signature is essentially the same problem.

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.