开发者

how do you shorten system.out.println in java

开发者 https://www.devze.com 2023-03-20 00:13 出处:网络
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()开发者_JAVA技巧)import static java.lang.Sy

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()开发者_JAVA技巧)


import static java.lang.System.out;

And in your code:

out.println("Foo");


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


Eclipse shortcut

In the Eclipse IDE, just type syso and then press Ctrl+space.


NetBeans shortcut

In the NetBeans IDE, type sout and press Tab key.

IntelliJ shortcut

In the IntelliJ IDEA IDE, do the same: Type sout, press Tab key.


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.


Autohotkey.ahk:

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

Then, press WIN+ALT+O.

Unfortunately, autohotkey only supports win32. :(


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 (j.u.l, 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 ;)


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


You might wanna try this

package com.company;

class Database {

    // SOP to print function
    public static void print(String inp) { 
        System.out.println(inp);
    }

    public static void print(int inp) {
        System.out.println(inp);
    }

    public static void print(double inp) {
        System.out.println(inp);
    }

    public static void print(float inp) {
        System.out.println(inp);
    }

    public static void print(boolean inp) {
        System.out.println(inp);
    }

    // SOPLN to printLN function

    public static void printLN(String inp) {
        System.out.println(inp);
    }

    public static void printLN(int inp) {
        System.out.println(inp);
    }

    public static void printLN(double inp) {
        System.out.println(inp);
    }

    public static void printLN(float inp) {
        System.out.println(inp);
    }

    public static void printLN(boolean inp) {
        System.out.println(inp);
    }

    public static void main(String[] args) {
        
        printLN("Hello");
        printLN(1);
        printLN(2.123);
        printLN(2.2);
        printLN(false);
        printLN(1+2);
        int x = 323;
        int y = 342;
        int z = x+y;
        printLN(x+y);
        printLN(z);
        printLN("Hello" + z);
        printLN("You can print anything.");

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消