How to make System.out.println() shorter

Java

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

Logging libraries

You could use logging libraries instead of re-inventing the wheel. Log4j for instance will provide methods for different messages like info(), warn() and error().

Homemade methods

or simply make a println method of your own and call it:

void println(Object line) {
    System.out.println(line);
}

println("Hello World");

IDE keyboard shortcuts

IntelliJ IDEA and NetBeans:

you type sout then press TAB, and it types System.out.println() for you, with the cursor in the right place.

Eclipse:

Type syso then press CTRL + SPACE.

Other

Find a "snippets" plugin for your favorite text editor/IDE

Static Import

import static java.lang.System.out;

out.println("Hello World");

Explore JVM languages

Scala

println("Hello, World!")

Groovy

println "Hello, World!" 

Jython

print "Hello, World!" 

JRuby

puts "Hello, World!" 

Clojure

(println "Hello, World!")

Rhino

print('Hello, World!'); 

Solution 2 - Java

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

Shortest. Go for it.

Solution 3 - Java

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 ;)

Solution 4 - Java

Some interesting alternatives:

OPTION 1

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

OPTION 2

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

Solution 5 - Java

For Intellij IDEA type sout and press Tab.

For Eclipse type syso and press Ctrl+Space.

Solution 6 - Java

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

LOG.info("foo")

Solution 7 - Java

In Java 8 :

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

Solution 8 - Java

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.

Solution 9 - Java

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;

Solution 10 - Java

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> :)

Solution 11 - Java

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!");

}

}

Solution 12 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEugeneView Question on Stackoverflow
Solution 1 - JavabakkalView Answer on Stackoverflow
Solution 2 - JavarupView Answer on Stackoverflow
Solution 3 - JavadbyrneView Answer on Stackoverflow
Solution 4 - JavaStephen PaulView Answer on Stackoverflow
Solution 5 - JavaMax GabderakhmanovView Answer on Stackoverflow
Solution 6 - JavaJon BringhurstView Answer on Stackoverflow
Solution 7 - JavaAbhilashView Answer on Stackoverflow
Solution 8 - JavadazitoView Answer on Stackoverflow
Solution 9 - JavaCaitlinGView Answer on Stackoverflow
Solution 10 - Javacarlos_lmView Answer on Stackoverflow
Solution 11 - JavaDenisView Answer on Stackoverflow
Solution 12 - JavaMarc TView Answer on Stackoverflow