Output variable value by using variable parameter in Java

Description

The following code shows how to output variable value by using variable parameter.

Example


//from ww w .j  av  a  2 s.com
import java.io.PrintStream;
import java.util.Date;

public class Main{

  public static void main(String[] args) {
    process(System.out, "Hello", "Goodbye");
    process(System.out, 42, 10, 17);
    process(System.out, "Foo", new Date(), new Object());
  }
  static void process(PrintStream out, Object ... args) {
    for (int i = 0; i < args.length; i++){
      out.println("Argument " + i + " is " + args[i]);
    }
  }
  static void process(PrintStream out, int ... args) {
    for (int i = 0; i < args.length; i++){
      out.println("Argument " + i + " is " + args[i]);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy