简体   繁体   中英

how do you shorten system.out.println in java

what is the absolute shortest possible shortcut to call System.out.println that makes it callable via the shortest possible number of characters (like print())

import static java.lang.System.out;

And in your code:

out.println("Foo");
public static void print(String s) { System.out.println(s); }

Via Eclipse, just type syso and then Ctrl+space

On Netbeans type sout and press TAB

Autohotkey.ahk:

#!o:: Send System.out.println("");{LEFT 3}

Then, press WIN+ALT+O .

Unfortunately, autohotkey only supports win32. :(

The shortest possible way would be to create a method with a short name. Something like:

public static void out(Object o){
    System.out.println(o.toString());
}

Now, you only have to call out("foobar"); in order to print something to standard out. The reason I used an object and the toString() method is so that and integer, for example, gets displayed properly.

Here's a workable example of the answers on this page, and also the related question . I'm not sure if any of these shortcuts is recommended for readable, reusable code.

import static java.lang.System.out; // only for method of minichate&Tim Cooper
import java.io.PrintWriter;// only for method of Stephan Paul

public class PrintExample{
    public static void main(String[] args){
        out.println("Typing of 7 characters saved!");
        p.pl("shortened System.out.println, 14 characters saved.");
        p.pl(77); // takes non-strings
        p.out(88); // also takes non-strings
        p.print("sorry, I only take strings!");
        //p.print(99); compilation error, int cannot be converted to String
        PrintWriter pr = new PrintWriter(System.out, true);
        pr.println(33); // method of Stephan Paul
    }
}

class p{
    // using generics (Java 5.0 onwards), by carlos_lm
    public static <T> void pl (T obj){
        System.out.println(obj);
    }
    // method by Neji3971 & bakkal, seems to work for all objects
    public static void out(Object o){
        System.out.println(o.toString());
    }
    // method by Jesus Ramos & rup. Only accepts strings!!
    public static void print(String s) {
        System.out.println(s);
    }
}

Use PhaseExpress free. It shourtcut anything for you.

If it's for the purposes of logging/debugging the logic of your program? The call that never happens.

Unless you are writing a console driven application, refrain from using System.out and System.err directly.

If you need logging, use a logging framework (jul, log4j, jcl, avalon, slf4j, logback, etc). These allow for improved context specific information, improved granularity and configuration at runtime.

To make it shortest you can write an another function and use it,but typing syso and clicking CTRL+Space is the best way I think.You should get used to using shortcuts when coding in JAVA;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM