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

static-import friendly convenience print methods

    Details

      Description

      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.

        Issue Links

          Activity

          Hide
          smarks Stuart Marks added a comment - - edited
          Initial draft of API and implementation is in the sandbox:

          http://hg.openjdk.java.net/jdk9/sandbox/jdk/rev/9d4b1dbb2800

          I'm not convinced that it's useful to write any tests for this. I've thus marked this bug as noreg-trivial.
          Show
          smarks Stuart Marks added a comment - - edited Initial draft of API and implementation is in the sandbox: http://hg.openjdk.java.net/jdk9/sandbox/jdk/rev/9d4b1dbb2800 I'm not convinced that it's useful to write any tests for this. I've thus marked this bug as noreg-trivial.
          Hide
          smarks Stuart Marks added a comment - - edited
          It is now possible to run "jshell --startup PRINTING" which will define print, println, and printf methods. However, this creates divergence between jshell and Java itself, so it can't be considered a solution. More thought is necessary on this.
          Show
          smarks Stuart Marks added a comment - - edited It is now possible to run "jshell --startup PRINTING" which will define print, println, and printf methods. However, this creates divergence between jshell and Java itself, so it can't be considered a solution. More thought is necessary on this.
          Hide
          smarks Stuart Marks added a comment -
          See extensive comments in JDK-8174145.
          Show
          smarks Stuart Marks added a comment - See extensive comments in JDK-8174145 .

            People

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

              Dates

              • Created:
                Updated: