[java] How to make System.out.println() shorter

Please advice on where can I find the lib in order to use the shorter expression of System.out.println() and where should I place that lib.

This question is related to java

The answer is


A minor point perhaps, but:

import static System.out;

public class Tester
{
    public static void main(String[] args)
    {
        out.println("Hello!"); 
    }
}

...generated a compile time error. I corrected the error by editing the first line to read:

import static java.lang.System.out;

void p(String l){
System.out.println(l);
}

Shortest. Go for it.


As Bakkal explained, for the keyboard shortcuts, in netbeans you can go to tools->options->editor->code templates and add or edit your own shortcuts.

In Eclipse it's on templates.


package some.useful.methods;

public class B {

    public static void p(Object s){
        System.out.println(s);
    }
}
package first.java.lesson;

import static some.useful.methods.B.*;

public class A {

    public static void main(String[] args) {

        p("Hello!");

    }
}

Java is a verbose language.

If you are only 3 days in, and this is already bothering you, maybe you'd be better off learning a different language like Scala:

scala> println("Hello World")
Hello World

In a loose sense, this would qualify as using a "library" to enable shorter expressions ;)


Using System.out.println() is bad practice (better use logging framework) -> you should not have many occurences in your code base. Using another method to simply shorten it does not seem a good option.


My solution for BlueJ is to edit the New Class template "stdclass.tmpl" in Program Files (x86)\BlueJ\lib\english\templates\newclass and add this method:

public static <T> void p(T s)
{
    System.out.println(s);
}

Or this other version:

public static void p(Object s)
{
    System.out.println(s);
}

As for Eclipse I'm using the suggested shortcut syso + <Ctrl> + <Space> :)


Some interesting alternatives:

OPTION 1

PrintStream p = System.out;
p.println("hello");

OPTION 2

PrintWriter p = new PrintWriter(System.out, true);
p.println("Hello");

Use log4j or JDK logging so you can just create a static logger in the class and call it like this:

LOG.info("foo")

In Java 8 :

    List<String> players = new ArrayList<>();
     players.forEach(System.out::println);

For Intellij IDEA type sout and press Tab.

For Eclipse type syso and press Ctrl+Space.