How to make System.out.println() shorter

后端 未结 12 875
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 07:10

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.

相关标签:
12条回答
  • 2020-12-04 07:25
    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!");
    
        }
    }
    0 讨论(0)
  • 2020-12-04 07:28

    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.

    0 讨论(0)
  • 2020-12-04 07:31
    void p(String l){
    System.out.println(l);
    }
    

    Shortest. Go for it.

    0 讨论(0)
  • 2020-12-04 07:32

    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;
    
    0 讨论(0)
  • 2020-12-04 07:34

    For Intellij IDEA type sout and press Tab.

    For Eclipse type syso and press Ctrl+Space.

    0 讨论(0)
  • 2020-12-04 07:35

    In Java 8 :

        List<String> players = new ArrayList<>();
         players.forEach(System.out::println);
    
    0 讨论(0)
提交回复
热议问题