New JAVA Proposal: Efforts to Simplify the Writing of Hello World

OpenJDK's JEP 445 proposal is trying to make getting started with Java easier. This proposal mainly introduces "flexible Main method and anonymous Main class", hoping that the learning process of Java will be smoother, so that students and beginners can better accept Java.

The author of the proposal, Ron Pressler, explained: The Java language is now  very suitable for developing and maintaining large and complex applications, but when schools teach programming, they often start with basic small programming concepts such as variables, functions and subroutines. At this stage, it is often not necessary to Large-scale programming concepts of classes, packages, and modules. But the current Java is not very friendly to beginners , such as the classic Hello, World! entry program:

    public static void main(String[] args) { 
        System.out.println("Hello, World!");
    }
}

In Ron's opinion, this code is too complex for a beginner's first program. Large programming constructs such as  class declarations and mandatory  public access modifiers are useful in encapsulating units of code with well-defined external component interfaces, but meaningless in this small starter example. The String[] args parameter is used to connect the code with external components, but will not be used inside this code. The static modifier is part of the Java class and object model, but it is too early to appear in Xinshoucun.

As an optimization, the proposal first enhances the protocol flexibility for launching Java programs:

  • Allows the main method of a started class to have public , protected , or default (ie package) access.
  • This method is called if the started class does not contain a static main method with a String[] parameter, but does contain a static main method without parameters.
  • If the started class has no static main method, but has a non-private zero-argument constructor (ie, public, protected, or package access) and a non-private instance main method, then construct an instance of that class. If the class has an instance main method with a String[] parameter, that method is called; otherwise, the instance main method is called with no parameters.

This allows  parameters main of methods  to be omitted String[] , and allows  main methods to be  public neither  static . Hello, World! can be simplified slightly:

class HelloWorld { void main() { System.out.println("Hello, World!"); } }

Additionally, an anonymous Main class is introduced to implicitly declare  class :

void main() { System.out.println("Hello, World!"); }

So far, Java's Hello, World! entry program has been simplified, but in Ron's eyes, the optimization provided by this JEP is only the first step to make Java easier to learn, and long methods/functions like System.out.println are also required Simplification, but these issues need to be gradually resolved in future JEP proposals.

This optimization is a preview language feature and is disabled by default. To try this example in JDK 21, the preview feature must be enabled: Compile  javac --release 21 --enable-preview Main.java the program with  java --enable-preview Main Run, or Run the program with the source launcher  java --source 21 --enable-preview Main.java .

More information about the flexible startup protocol and anonymous main class can be found in the proposal text .

Proposal goal

  • Provides a smooth entry into Java so educators can introduce programming concepts step-by-step.

  • Helps students write basic introductory programs in a concise manner, and expand the code gracefully as their Java skills grow.

  • Reduce the ritual of writing simple programs such as scripts and command-line utilities.

  • Don't introduce beginner dialects of Java alone.

  • No separate starter toolchain is introduced; students' programs should be compiled and run with the same tools as production Java programs.

Guess you like

Origin www.oschina.net/news/236836/jep-445