7 ms·
Maybe my favorite feature in this release: https://openjdk.org/jeps/463 https://openjdk.org/jeps/463 Finally solves the inscrutable Hello World program! Yes,
by jimbokun 3y ago
Maybe my favorite feature in this release:
https://openjdk.org/jeps/463 https://openjdk.org/jeps/463
Finally solves the inscrutable Hello World program!
Yes, it's just ergonomics for early beginners. But could be the difference in whether or not someone new to programming sticks with Java or not.
- kyawzazaw 3y agolove this!
- carimura 3y agoThe thought process leading up to this work https://openjdk.org/projects/amber/design-notes/on-ramp https://openjdk.org/projects/amber/design-notes/on-ramp
- coldpie 3y agoI 1000% agree. Though, a favorite early programming memory of mine was when static methods finally clicked and I went back and looked at all that boilerplate that meant nothing to me and it all became crystal clear what it meant and why it had to be like that.
- eddd-ddde 3y agoThis reminds me of when I finally understood why returning 0 from a function named main made sense.
- fuzztester 3y agoAnd why array indices start at 0 instead of 1, in many, though not all, programming languages.
- fuzztester 3y agohttps://en.m.wikipedia.org/wiki/Zero-based_numbering https://en.m.wikipedia.org/wiki/Zero-based_numbering
- lotsoweiners 3y agoThat sounds a lot like Top-level Statements in the .Net world. Personally I don’t use that feature because I’m old and get confused if things are magic but can certainly see the benefit for the newer devs.
- starik36 3y agoYeah, no doubt inspired by the .NET feature. I use it all the time for throwaway console apps.
- jonathanlydall 3y agoI’m also a somewhat old school C# dev and the main reason I don’t use top level statements is that they only work in a single file of an application, so that single file is different from every other file in your solution. It’s okay if you’re only writing a single file, and it really seems intended for beginners or micro applications, but for those of us writing anything besides the most trivial, it’s merely “cute” and just adds inconsistency.
- Almondsetat 3y agoMy first Java programming course went over in detail about that piece of code and in a couple of hours taught me a lot of important Java concepts right away. Consider me not enthused about it.
- whitehexagon 3y agoSimple code like that kept pulling me back to the JSL 1.0 spec time and time again as my understanding deepened, much like the old singleton pattern debates of old.
- AtlasBarfed 3y agoJava continues to evolve to a bad Groovy.
- bheadmaster 3y agoGroovy itself is a bad Groovy.
- AtlasBarfed 3y agobad opinion is bad I get gradle is a disaster (but that's more of a poorly managed and evolved DSL problem) But ... does anyone use Spock and think "groovy sucks"? Yeah, I doubt it.
- svieira 3y agoI hate to be "that guy" but ... raises hand. Spock is also a DSL disaster that's trying to be ScalaTest (https://www.scalatest.org/user_guide/property_based_testing https://www.scalatest.org/user_guide/property_based_testing) but in a dynamic language. Every time I use it I have to re-learn the syntax. (I have the same issue with Gradle, so maybe it's just-me?)
- bombcar 3y agoMy problem with gradle is I never use it enough to get familiar with it, once the problem is solved I promptly forget everything.
- AtlasBarfed 3y agoIt's the perfect example of a bad DSL. A "DSL" like SQL that you use hundreds of times a year you'll learn the model of. A DSL like groovy that you use once for project setup (and for that you'll one-off it likely and stackoverflow the rest of the question) is not. And it isn't really natural, it's a bunch of arcane steps that at the end might superficially look a bit consistent, but still really isn't. Groovy really needs an autocompleter or a generator. Or, well, as you say, you stackoverflow for everything outside the very vanilla basics of it. But good luck getting the right version match to the syntax you need. Christ.
- bheadmaster 3y agoGood that they got rid of the class/public static void/string[] Args boilerplate, but if they just went one step forward and declared implicit main function as top-level scope of a file, we could've got to a Python level of tersity. Just imagine! System.out.println("Hello world!");
- aidos 3y agoTo be that guy, not quite a python level of tersity!
- fuzztester 3y ago# Python 3 p = print # the print() function # Now we can do: p("Hello world") # endlessly, in this file. Shorter by 17-odd characters than the above Java version.
- bheadmaster 3y agoWell, if they added System.out in the default context, it could've been just println("Hello world!") I think that's as far as you can go.
- int_19h 3y agoThey almost caught up with C#, which ditched requisite class declarations years ago. Although C# went one step further and allowed top-level code outside of methods, so that hello world is now: Console.WriteLine("Hello, world!")
- TillE 3y agoIt has been a few years, but top-level statements in C# are still a fairly recent thing (C# 9 in 2020).
- johnyzee 3y agoYou can write write code outside of methods in a Java class, called an initializer block: class MyClass { { System.out.println("Hello World!"); } } This will run when an object of the class is instantiated. So not useful for the case in question (you could get rid of the class declaration but would presumably still need a main method). (It's also pretty poor style, but occasionally useful in a pinch, like when instantiating an anonymous inner class, which has no constructors that take arguments)
- HackerThemAll 3y agoC#'s got even better version of this feature since Nov 2021. Hello world is just a true one-liner. https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals...
- masfoobar 3y agoI really disliked this the first time I saw it. I was like "WTF is this?" I can only think the reason why they did this was to compete with python, as our scripting languages that do not require a main/Main entry point. Personally, it just feels wrong for compiled-based languages. I think they could improve on C# with their namespace. Rather, for example, a typical C# file looking like:- using EFG; namespace ABC { public class XYZ { } } They could just :- using EFG; using namespace ABC; public class XYZ { } Pretty much taken from the C++ way.. but it saves on space availability.
- sfn42 3y agoNamespace declarations have been a thing in C# for a while
- masfoobar 3y agoYep. Point is it should have been added since Framework 4.
- neonsunset 3y agoThe second option is what C# defaults to since a few versions ago.
- masfoobar 3y agoCorrect -- was added since .net 6, I believe.
- sireat 3y agoActually a nice feature. This solves the public static void main string args issue. Still if I need JVM then Scala or Kotlin are still preferable over Java. Obligatory Java for the Haters in 100 Seconds: https://www.youtube.com/watch?v=m4-HM_sCvtQ https://www.youtube.com/watch?v=m4-HM_sCvtQ
- willsmith72 3y agoI've never seen a java codebase I thought couldn't be better in kotlin
- mdaniel 3y agoFor reading it, I believe that. For authoring it, yikes it is slow. I'd guess due to all the implicit and implied and coercion and ... and ... and. Now I'm cognizant that I'm running my mouth without trying it on 1.9.x and a similarly recent IJ so maybe it got infinitely better, but my life experience with languages is that they only get more fancy swooshings not QoL improvements due to Moore's Law and yearly refresh cycles I was/am always glad to pay that tax because non-null-by-default and the vastly superior val/var keyword pairs are totally worth it but it for sure is not a drop-in replacement in team environments. I am already constantly battling the vscoders (to say nothing of vimers) whining that "IJ is too slow for my eeee-leeeet typin'"
- Nashooo 3y agoDefinitely also for writing it.
- pjmlp 3y agoLooking forward to JetBrains proposal to rewrite Java Virtual Machine into Kotlin Virtual Machine, certainly the existing Java codebase would be largely improved.
- blackoil 3y ago> Still if I need JVM then Scala or Kotlin are still preferable over Java. That's so 2010. I would avoid Scala like plague. It is a soup of all features imagined and some more. Java is difficult for novice. With Scala even experienced senior devs can look into a snippet and be befuddled. It is designed by academicians who thought clever looking choice is better.
- aembleton 3y agoMaybe in another ten years they'll remove System.out.println and allow for a simple println to be used. Or maybe we'll all move to Kotlin by then.
- chopin 3y agoYou can statically import System.out if you are so inclined.
- aembleton 3y agoYou can but for a new user that is even more of a barrier than writing out System.out.println
- gdejohn 3y agothis will probably show up in java 23, ctrl + f "simpleio" https://openjdk.org/jeps/8323335 https://openjdk.org/jeps/8323335
- pjmlp 3y agoAnd create the Kotlin Virtual Machine, with a Kotlin based ecosystem?
- bitwize 3y agoThis is exactly what I've been calling the "public static void main problem" for years (and an illustration of why I thought something like BASIC or Python should be everyone's first programming language). Neat to see the Java team come up with a solution for it. But "public static void main" runs deeper, and has to do with immediacy: shortening the time between the programmer giving instructions to the computer and seeing the results of those instructions being executed. It makes the smallest possible Java program greatly simpler and absent unusual comments that beginning programmers might struggle with; but there's the issue of the compilation step as well! If they make Java REPL-driven they might well have a winner!
- craigacp 3y agoJava has had the ability to run a single code file directly by running `java <path-to-file.java>` since Java 11 - https://openjdk.org/jeps/330 https://openjdk.org/jeps/330. And it's had a Java REPL since Java 9.
- dajt 3y agoI really doubt that. Java is complicated enough that it's not beginner friendly even if you don't have to write the class to put main into. Given these are still methods of classes this JEP seems pointless to me. Not having to write class x {} isn't a great time saver.
- cryptos 3y agoThis is nice for beginners, but the confusion is only postponed, because then you'd have to learn why you need to write a class as a container for static methods and that these methods must be static. So, the Java developers created another special case in the language. Compare that with Kotlin where you have top-level functions, so that `main` is just like any other function.
- okeuro49 3y agoI really dislike this feature. It's better to just say "we will come to this concept later", rather than make a fake syntax that does this (taken from the JEP) behind the scenes: new Object() { // the implicit class's body }.main(); It adds more confusion, as you are left explaining that you could not really run an instance method without instantiating the class -- it was just something fake for beginners.
- Shakahs 3y agoI agree. Coming from NodeJS I appreciate how explicit Java is. The complexity is there either way, but in Java it's right up front where I can see it, not hidden away under layers of abstraction for a supposedly better DX.
- jonahbenton 3y agoWith respect, not hostile to beginners but adding complexity to the build to support this kind of implicit sugar, when beginners are 100% going to be using AI to write code and answer questions- seems like not the right tradeoff in 2024.
- fuzztester 3y agoWell, they reduced a few lines of boilerplate for hello world Java program, while adding many many paragraphs of text to explain it. ;)