javaconsoleconsole-application

Newer easier ways to interact with the console in Java 23+?


I have seen mentions of new classes and methods, like this, for interacting with a console in a Java app.

I cannot find a Java JEP for that on the JEP index.


Solution

  • tl;dr

    Yes, new console methods are being previewed in Java 24 (& Java 23).

    The existing Console class, and a new IO class, offer print, println, & readln methods.

    The old System.out.println ( "Hello world!" ) is supplanted by the new IO.println ( "Hello world!" ).

    Details

    Yes, there is a series of JEPs adding features related to the console.

    This series is part of the Paving the On-Ramp effort in recent years to make Java easier for beginner students.

    java.io.IO

    If successful, this series of JEPs would add a new class, java.io.IO. This class offers these methods:

    Notice that these methods are static. So we can use them without instantiating an object.

    So the familiar System.out.println ( "Hello world!" ); becomes:

    import java.io.IO;
    …
    IO.println ( "Hello world!" );
    

    Or, be even more brief with a static import.

    import static java.io.IO.println;
    …
    println ( "Hello world!" );
    

    java.io.Console

    These java.io.IO methods are implemented by calling upon the Console object returned from System.console(). So that Console class gains new methods, with the same signatures.

    More info

    Caveat: These are preview features, subject to change or removal.

    Try these out in an early-access build of Java 24 based on the OpenJDK codebase. See the early-access Javadoc for Java 24.