IT story

System.out.println ()을 짧게 만드는 방법

hot-time 2020. 7. 12. 09:33
반응형

System.out.println ()을 짧게 만드는 방법


더 짧은 표현을 사용하기 위해 lib를 찾을 수있는 System.out.println()위치와 해당 lib를 어디에 배치해야하는지 조언하십시오 .


로깅 라이브러리

휠을 다시 발명하는 대신 로깅 라이브러리를 사용할 수 있습니다. Log4j는 예를 들어이 같은 다른 메시지에 대한 방법을 제공 할 것입니다 info(), warn()하고 error().

수제 방법

또는 단순히 println자신 만의 방법을 만들고 호출하십시오.

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

println("Hello World");

IDE 키보드 단축키

IntelliJ IDEA 및 NetBeans :

사용자가 입력 sout을 누른 다음을 누릅니다 TAB유형을, 그리고 System.out.println()바로 이곳에서 커서로, 당신을 위해.

식:

입력 syso한 다음 CTRL+ 를 누릅니다 SPACE.

다른

좋아하는 텍스트 편집기 / IDE 용 "스 니펫"플러그인 찾기

정적 임포트

import static java.lang.System.out;

out.println("Hello World");

JVM 언어 탐색

스칼라

println("Hello, World!")

그루비

println "Hello, World!" 

자이 썬

print "Hello, World!" 

JRuby

puts "Hello, World!" 

클로저

(println "Hello, World!")

코뿔소

print('Hello, World!'); 

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

가장 짧습니다. 해봐


Java는 자세한 언어입니다.

3 일만에 이미 귀찮게된다면 스칼라와 같은 다른 언어를 배우는 것이 좋습니다.

scala> println("Hello World")
Hello World

느슨한 의미에서 이것은 짧은 표현을 가능하게하기 위해 "라이브러리"를 사용하는 것으로 간주됩니다.)


몇 가지 흥미로운 대안 :

옵션 1

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

옵션 2

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

대한 하게 IntelliJ IDEA의 유형 sout을 누릅니다 Tab.

들어 이클립스syso누릅니다 Ctrl+ Space.


log4j 또는 JDK 로깅을 사용하면 클래스에서 정적 로거를 작성하고 다음과 같이 호출 할 수 있습니다.

LOG.info("foo")

Bakkal이 설명했듯이 키보드 단축키 netbeans에 대해서는 도구-> 옵션-> 편집기-> 코드 템플릿으로 이동하여 자신의 단축키를 추가하거나 편집 할 수 있습니다.

에서 Eclipse이 템플릿입니다.


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


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;

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

    }
}

In Java 8 :

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

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.

참고URL : https://stackoverflow.com/questions/3320764/how-to-make-system-out-println-shorter

반응형