• JackbyDev
    link
    fedilink
    English
    510 hours ago

    This is getting a little better nowadays.

    > cat Hello.java
    void main() {
        System.out.println("Hello, World!");
    }
    > java --enable-preview Hello.java
    Hello, World!
    

    Things to notice:

    1. No compilation step.
    2. No class declaration.
    3. Main method is not public static
    4. No String[] args.

    This still uses preview features though. However, like you demonstrated already, compilation is no longer a required step for simplistic programs like this.

    • مهما طال الليل
      link
      fedilink
      1
      edit-2
      2 hours ago

      Main method is not public static

      It must be somewhere under the hood. Otherwise, it wont be callable and it would require an instance of an object to call. Unless the object here is the Java environment?

      No String[] args

      They are just optional I’m sure, like C and C++. You still need them to read command line arguments.

      All in all, these syntax improvements are welcome. I already moved on to Kotlin for Android development though.

      • JackbyDev
        link
        fedilink
        English
        1
        edit-2
        58 minutes ago

        Main method is not public static

        It must be somewhere under the hood. Otherwise, it wont be callable and it would require an instance of an object to call. Unless the object here is the Java environment?

        No. From JEP-445:

        If an unnamed class has an instance main method rather than a static main method then launching it is equivalent to the following, which employs the existing anonymous class declaration construct:

        new Object() {
            // the unnamed class's body
        }.main();
        

        No String[] args

        They are just optional I’m sure, like C and C++. You still need them to read command line arguments.

        Without the preview feature enabled, it is not an optional part of the method signature. It specifically looks for a main(String[]) signature.

        • مهما طال الليل
          link
          fedilink
          122 minutes ago

          I am not in the mood to read a technical document, but I don’t think the resulting binary/byte code should be different between the two “hello world” programs. But then again, why not?

          Without the preview feature enabled, it is not an optional part of the method signature. It specifically looks for a main(String[]) signature.

          Ah ha! So that’s what’s going on here. They almost got it right. They had the potential to make a lot of the boilerplate optional or implicit under relevant circumstances, but instead the language has two explicit switchable modes.

          Can I write a Java application in “preview feature”?

    • @cashew
      link
      4
      edit-2
      7 hours ago

      Microsoft Java is a one-liner these days.

      > cat program.cs
      Console.WriteLine("Hello, World!");
      > dotnet run
      Hello, World!