Get rid of System.out

Or how to replace annoying System.out.println with concise println

Some features of Java are annoying for me, especially after several years of Scala experience. First of all: why should I use System.out.println instead of println!

You won’t find such a lengthy construction in other languages derived from Java: Scala, Groovy or Kotlin

It appears , that this problem can be solved easily. I wrote a tinny library.


package ru.exxo.jutil;

import java.util.Locale;
public interface Printer {

    static  void print(T t) {
        System.out.print(t);
    }

    static  void println(T t) {
        System.out.println(t);
    }

    static void printf(String str, Object... objects) {
        System.out.printf(str, objects);
    }

    static void printf(Locale locale, String str, Object... objects) {
        System.out.printf(locale, str, objects);
    }
}

Now one can import these static methods:


import static Printer.*;

And forget of System.out for good


public class PrinterDemo {
    private static class Demo {
        public Demo(String str) {
            this.str = str;
        }

        private String str;

        @Override
        public String toString() {
            return "Demo{" +
                    "str='" + str + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {

        Demo demo = new Demo("It's a Demo");
        println("I can print!");
        println(10);
        println(true);
        println(demo);

        printf("It's %TA, %<tH:%<tM. It's %s", new Date(), "good");
        printf(Locale.forLanguageTag("RU"), "%n%,.2f", 10000.5);
    }
}

After a month of using “new style of printing” I loaded this lib to the Maven Central.

Maven


<dependency>
     <groupId>ru.exxo.jutil</groupId>
     <artifactId>printer</artifactId>
     <version>1.2</version>
 </dependency>

Gradle


implementation 'ru.exxo.jutil:printer:1.2'

See it on github

Please follow and like us:

About Alexandre Kremlianski

Scala / Scala.js / JavaScript programmer

COMMENTS to Raju

Leave a Reply to Raju Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.