Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Java: How to make a short alias for System.out.println()

Tags:

java

I would like to make an alias of or an extended version of System.out.println() for printing my variables of various types. How does one pass an argument with unknown type/class to a method?

public static void p(VariableType... args) {
    System.out.println(args[0]);
    // ...
}
like image 606
Andrei Avatar asked Jul 02 '11 15:07

Andrei


2 Answers

You can use Object.

public static void p(Object... args) {
  System.out.println(args[0]);
  // ...
}
like image 141
Marcelo Avatar answered Oct 12 '22 22:10

Marcelo


Unless you want lots of lines in the output, you could do.

public static <PrintableToString> void p(PrintableToString... args) {
    for(PrintableToString pts: args)
        System.out.print(pts);
    System.out.println();
}
like image 27
Peter Lawrey Avatar answered Oct 12 '22 22:10

Peter Lawrey