• JackbyDev
    link
    fedilink
    English
    arrow-up
    7
    ·
    1 month 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.

    • cashew@lemmy.world
      link
      fedilink
      arrow-up
      4
      arrow-down
      1
      ·
      edit-2
      1 month ago

      Microsoft Java is a one-liner these days.

      > cat program.cs
      Console.WriteLine("Hello, World!");
      > dotnet run
      Hello, World!
      
    • Valmond@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      1 month ago

      System.base.stuff.output.out.printfunctions.println

      Or so it felt every time you wanted to dump something into the console…

    • مهما طال الليل@lemm.ee
      link
      fedilink
      arrow-up
      1
      arrow-down
      2
      ·
      edit-2
      1 month 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
        arrow-up
        2
        ·
        edit-2
        1 month 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.

        • مهما طال الليل@lemm.ee
          link
          fedilink
          arrow-up
          2
          arrow-down
          6
          ·
          1 month 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”?

          • JackbyDev
            link
            fedilink
            English
            arrow-up
            7
            ·
            edit-2
            1 month ago

            I mentioned this uses preview features twice in the first comment regarding this, so I don’t know why you’re "ah ha"ing. Also you don’t need to read the technical document, I’ve quoted the entirety of the relevant text. I provided it as a citation.

            You seem confused about preview features. It’s not a switchable mode to reduce boiler plate. I find the name very clear, but here is more information. From JEP-12

            A preview feature is a new feature of the Java language, Java Virtual Machine, or Java SE API that is fully specified, fully implemented, and yet impermanent. It is available in a JDK feature release to provoke developer feedback based on real world use; this may lead to it becoming permanent in a future Java SE Platform.

            As an example, JDK 17 added pattern matching for switch statements as a preview, and by JDK 21 it was added as a full fledged feature that doesn’t require usage of the enable preview flag. Presumably in some future release of Java this feature will not require the usage of a flag.

            • مهما طال الليل@lemm.ee
              link
              fedilink
              arrow-up
              1
              arrow-down
              2
              ·
              edit-2
              1 month ago

              It is pretty late for me. Sorry. And thank you for your patience. Repeating it three times helped.

              It will be interesting to find out if the resulting binary is the same or not and what’s possible once it matured.

              • JackbyDev
                link
                fedilink
                English
                arrow-up
                3
                ·
                1 month ago

                Yes, because it’s genuinely not a static method. It’s an instance method. Also the signature is different. It’s not some sort of mere syntactic trick that translates void main() to public static void main(String[] args).