Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8130086

static-import friendly convenience print methods

XMLWordPrintable

      Any small program in Java must include a bunch of boilerplate that defines a class with a static main() method with the right arguments. This presents noise and difficulty for beginning programmers. The Java Shell (aka REPL, aka Project Kulla; see JDK-8043364) alleviates most of the pain associated with this boilerplate.

      The simplest program usually just prints a string. This is accomplished via a single statement:

          System.out.println("Hello, world!");

      This still presents a lot of complexity to beginners, namely:

       - System is a predefined class
       - out is a static field of that class
       - println is an instance method of 'out'

      A static import of System.out would reduce this to

          out.println("Hello, world!");

      However, 'println' itself cannot be statically imported, since instance methods cannot be statically imported.

      It would be preferable simply to be able to write

          println("Hello, world!");

      This would make things much nicer for beginning programmers, particularly those using the REPL. In addition, it's quite common for non-beginner programmers to define their own utility methods so that they don't have to type "System.out" all the time.

      To mitigate these issues, the following is proposed:

      Add a class java.lang.Out (or java.io.Out) that has a set of static methods suitable for static import:

       - print
       - println
       - printf

      These are simply convenience methods for invoking the corresponding methods on System.out.

      All overloads of the print* methods should be included. Although their usefulness varies, there seems to be no good reason to omit any of them.

      Similar convenience methods for System.err are probably unnecessary, as the error output is considerably less common for programs that want to print things. Other methods on PrintStream such as append(), close(), and flush() are also likely unnecessary.

      Putting the new Out class in java.lang makes it possible to use directly without any imports at all. This is an advantage over putting it into java.io, where it would logically seem to belong.

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: