Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've been using Java 21 for Advent of Code this year, and I've been surprised at how much less clunky it is than I remember. `var` and `record`'s are especially nice for how I like to code. It's still not perfect, but it's not something that I hate programming in now.

These were some neat tricks. I've been using the `java myfile.java` approach for running the AoC problems. I didn't realize the "implicit class" thing where you could have top level statements and methods, though, with the automatic import of java.base. That's pretty slick for this.



Records being allowed in methods has been great for me when doing things like path finding. Makes it easy to write something like:

    record Node(int cost, int row, int col){}
    var pq = new PriorityQueue<Node>(Comparator.comparingInt(Node::cost));


This is actually a natural extension of classes being allowed inside, which is not well-known for some reason. Nonetheless, I also really like this feature with records.


> which is not well-known for some reason

Really? A whole generation of Java programmers wrong functor classes in-line.


Those are anonymous classes, if we‘re thinking of the same thing. Java also supports named classes bound to a method‘s scope (local classes).


It's allowed because Record automatically implements methods called cost(), row(), and col().


So it’s almost as good as C#? Which would be my favorite language if not for strong ties to MS ecosystem.


Just to be clear, the implicit class thing is still a preview feature as of Java 24, which should come out next March. It might be finalized for Java 25, in the coming September. But no guarantees. Until then you'd need to explicitly enable it with a cli parameter.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: