Readit News logoReadit News
jimbokun · 2 years ago
Maybe my favorite feature in this release:

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.

HackerThemAll · 2 years ago
C#'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...

masfoobar · 2 years ago
I 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.

coldpie · 2 years ago
I 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 · 2 years ago
This reminds me of when I finally understood why returning 0 from a function named main made sense.
sireat · 2 years ago
Actually 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

blackoil · 2 years 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.

willsmith72 · 2 years ago
I've never seen a java codebase I thought couldn't be better in kotlin
okeuro49 · 2 years ago
I 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 · 2 years ago
I 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.
carimura · 2 years ago
The thought process leading up to this work

https://openjdk.org/projects/amber/design-notes/on-ramp

Almondsetat · 2 years ago
My 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 · 2 years ago
Simple 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.
cryptos · 2 years ago
This 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.

int_19h · 2 years ago
They 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!")

johnyzee · 2 years ago
You 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)

TillE · 2 years ago
It has been a few years, but top-level statements in C# are still a fairly recent thing (C# 9 in 2020).
lotsoweiners · 2 years ago
That 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.
jonathanlydall · 2 years ago
I’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.

starik36 · 2 years ago
Yeah, no doubt inspired by the .NET feature. I use it all the time for throwaway console apps.
bitwize · 2 years ago
This 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 · 2 years ago
Java 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. And it's had a Java REPL since Java 9.
bheadmaster · 2 years ago
Good 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 · 2 years ago
To be that guy, not quite a python level of tersity!
dajt · 2 years ago
I 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.

aembleton · 2 years ago
Maybe 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 · 2 years ago
You can statically import System.out if you are so inclined.
gdejohn · 2 years ago
this will probably show up in java 23, ctrl + f "simpleio"

https://openjdk.org/jeps/8323335

pjmlp · 2 years ago
And create the Kotlin Virtual Machine, with a Kotlin based ecosystem?
jonahbenton · 2 years ago
With 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 · 2 years ago
Well, they reduced a few lines of boilerplate for hello world Java program, while adding many many paragraphs of text to explain it. ;)
kyawzazaw · 2 years ago
love this!
AtlasBarfed · 2 years ago
Java continues to evolve to a bad Groovy.
bheadmaster · 2 years ago
Groovy itself is a bad Groovy.
ulrikrasmussen · 2 years ago
How so? I don't think there are many good things to say about Groovy, but I generally agree with most new features introduced to Java. What do you think Groovy does better?
ecshafer · 2 years ago
It isn't a "Sexy" PL change, but a full foreign function interface will be a huge change. In my experience, relying on the old java JNI based libraries seems to be one of the biggest things that break in upgrades. So I am hoping this will reduce the maintenance burden of Java.
DarkNova6 · 2 years ago
Boring changes like these are what keeps Java interesting.

New and shiny syntax sugar becomes stale real quick.

owlstuffing · 2 years ago
As a modern language Java is still quite feature deficient. But you can use compiler plugins like the manifold project[1] to level up.

1. https://github.com/manifold-systems/manifold

krzyk · 2 years ago
Linkable features https://openjdk.org/projects/jdk/22/

For me personally the most interesting one is JEP461 (Stream Gatherers) https://openjdk.org/jeps/461

This will allow addition of interesting stream operations.

cryptos · 2 years ago
This is indeed nice. However, static extension methods would have made this feature simpler (and more generic).
krzyk · 2 years ago
And would also make code harder to read. I was always reluctant to use those in Scala because of this.
cryptos · 2 years ago
Although I like Kotlin better, I'm really impressed how Oracle continues to improve Java. Some developers think Java is an old, cumbersome language, but it is indeed such a productive language and ecosystem with outstanding quality! Java has quite good support for functional programming, concise and immutable structs (called "records"), pattern matching, string templating, virtual threads, structured concurrency (preview for now), a vast ecosystem with outstanding quality, world class documentation, a fast and robust runtime and so on.

Even after nearly 30 years, Java is still a good choice to start something new (e.g. a startup).

munksbeer · 2 years ago
It is very trendy to hate java. Which is actually a good thing, because it means I get to continue to enjoy working with java alongside sensible professional developers, who quitely get on with writing excellent code and creating industry leading systems with java. Less competition for the roles and I don't have to work with fanboy types.
oaiey · 2 years ago
Foreign Function Interface is one of the key advantages what C# had over Java (e.g. against huge APIs like Android or macOS). I am really curious how this JEP turns out.
neonsunset · 2 years ago
Small correction: FFI in Android land is abysmal, to get decent performance when accessing all kinds of system APIs you almost always end up having to write helpers in Java, to try to alleviate the pain of JNI as much as possible (it is still significant).

Interop with iOS OTOH is a breeze, some of the APIs may require newer Swift Library Evolution ABI (direct calls to Swift exprots, without going through C/objC) but .NET is getting it in .NET 9 (the first and only platform to have it at the moment of release).

pron · 2 years ago
Calling Android "Java" has always been a stretch. Not only has Android never been compatible with any version of Java, the divergence between Android and Java is only growing. JDK 22 is Java; Android is Android.
adra · 2 years ago
I started to tinker with this new API last week. I definitely think it's a lot better than needing to self compile a lot of the bindings yourself which will certainly bring more from the community into the feature than were here before.

That said, you really need to write a bunch of boiler-plate to implement useful things in java-like paradigms in the library today. Just as an example, I wrote up a sane windows _getcwd() stub and it's like 30 lines to implement correctly (properly freeing the buffer when I'm down with it)

My pet project is to see if I can write automatic stubs around calls based on published API info and sources, so that at least some of that platform binding lift isn't just for those with non-trivial knowledge of C. Well, that's the hope anyways.

schoenobates · 2 years ago
I've been quite keen on the new FFI too - apologies if this is old news, but have you tried the [0] jextract stuff for some of the boilerplate gubbins?

[0]: https://github.com/openjdk/jextract

ptx · 2 years ago
Have you looked at Microsoft's Win32 API metadata package [0]? They're using it to generate C# and Rust bindings, and other people have targeted other languages.

[0] https://github.com/microsoft/win32metadata

soulbadguy · 2 years ago
The FFI is also an artifact of CLR type system and the ability of have proper stack value based types.
int_19h · 2 years ago
Not just value types, but also:

1. Pointers to the same (and not just references to objects).

2. Unions (via explicit-layout structs)

3. Function pointers.

4. C-style varargs.

To be fair, not all of these have been exposed in C# historically even though CLR had them all along. Most notably, unmanaged function pointers took over 20 years. And since most people look at CLR through the prism of C#, they aren't necessarily aware of these features.

Still, one way or the other, we're at the point where pretty much any C program can be transpiled to C# in a one-to-one mapping of almost every construct (setjmp/longjmp has to be emulated with exceptions), and a very similar performance profile of the resulting code.

merb · 2 years ago
Sadly records are not value types, maybe Java will do this better, but I doubt it. Valhalla is probably years away.
grishka · 2 years ago
They will eventually bring value types to Java with Project Valhalla.
MaxBarraclough · 2 years ago
For anyone reading, here's a link to the page with detail on this project: https://openjdk.org/jeps/454
zbentley · 2 years ago
It's definitely exciting and a big step forward from JNI/JNA.

However, I am a little concerned about the heavy emphasis on library unloading in the JEP. Unloading is famously fraught and unsafe for wide swaths of common libraries ... or maybe I'm overfitting due to recent trauma[1] and everything will be fine.

1. https://gist.github.com/thomcc/8c4a8003cf1a282949a539e899cb4...

ackfoobar · 2 years ago
It worked great for me. I had long wanted to rewrite a Python lib in Kotlin, since the Python part of the lib is slow, and it does not support multi-threading.

But the ugliness of JNI stopped me. Then I tried FFI in Java 21, with jextract it was amazing.

I wasn't aware of FFI at first, but the finalized version of virtual threads made me check the Java release notes.

preommr · 2 years ago
It's kind of startling to see how many places still use Java 8, estimated at ~1/3 of projects according to a survey i just googled. And something like half that still use java 11.
MBCook · 2 years ago
8 reached EOL in the last year or two. I think 11 is still under LTS.

Part of the problem is the changes made between 8 and 17 could be incredibly disruptive depending on your libraries.

If they had stuck to not using sun.* stuff you were pretty safe, but a few very popular things did.

Then somewhere in there a lot of the common server stuff moved from javax.* over to jakarta.*.

Both were needed and very good, but could turn into dependency nightmares. So depending on your app/stack it wasn’t an easy transition.

17 is fantastic though, especially coming from 8. I’m excited about what’s already in 21 (latest LTS) and coming in the next LTS after that.

agurk · 2 years ago
> 8 reached EOL in the last year or two

Java 8 is still under Extended Support until 2030 (and indefinite sustaining support). Java 11 left Premier Support September 2023.

"Java SE 8 has gone through the End of Public Updates process for legacy releases. Oracle will continue to provide free public updates and auto updates of Java SE 8 indefinitely for Personal, Development and other Users via java.com".

https://www.oracle.com/java/technologies/java-se-support-roa...

fngjdflmdflg · 2 years ago
Wasn't the whole point of Java never releasing a 2.0 that there would never be breaking changes? Seems like something went very very wrong with Java if the state they are in is that the language refuses to make breaking changes and the developers refuse to take up the supposedly non breaking updates due to breaking changes based on the comments I'm seeing here. So the language never improves as much as it can and the developers never update anyway making the whole point of non-breaking changes moot.
chungy · 2 years ago
Java 8 allows access to unsafe methods, which despite being meant for internal JDK use only, have been widely used in external projects. Newer Java versions do not permit the use of unsafe except for the JDK itself.

It wasn't ever part of the public API that's promised to never break, and JDK developers were annoyed that it being used meant Java got perceived as unstable, and it's gone in more recent versions.

kaba0 · 2 years ago
There are barely any breaking changes, I could still run a graphical application written by a prof in early 2000s, served on his website as a jar. No other language comes remotely as close to this level of stability.

As for the changes in 8->9, there was a stagnation period in the last year of Sun, and many people overfitted their libs to 8, by e.g. making use of internal APIs made accessible through reflection. This was never a good idea, and all that is happening now is that you have to explicitly state at invocation what kinds of non-future-proof modules you make use of, so that you are made aware of what could potentially change, plus it disincentivizes libraries from accessing these non-public APIs.

adra · 2 years ago
I'm dragging my new company's codebases from 8 to 17 from soup to nuts. It's not always just about the language update, but all the old legacy crust that accumulated since java 8 that prevented anyone else to do it. My PRs are terrible and necessary.
elric · 2 years ago
Wait until you hear about COBOL still being used.

Snark aside, the problem with Java in the enterprise is that Oracle provides support for mind-bogglingly ancient versions (if you're willing to pay them). This disincentivizes companies from upgrading, ends up frustrating their developers, and has probably contributed to the negative reputation that Java has in some corner of the net.

I get that tracking the current version isn't feasible for most companies, especially if software isn't their core business. The Java folks do their best to make upgrades painless, but the longer you wait, the more painful it gets.

BroomOfSYS · 2 years ago
Unbelievable! It is a strong signal that people don’t care and technical leadership is bad. Always the nonsense excuses of having to work on a “legacy” codebase for more than ten years with a team of 3 developers and 4 analysts.
Nullabillity · 2 years ago
In practice, Java is the language of legacy and Android.

Java 8 keeps working, while "modern" Java keeps chasing mistakes like green threads. If anything, I'm more baffled by 9+ having a non-neglegible market share at all.

citrin_ru · 2 years ago
> Java keeps chasing mistakes like green threads

If by green threads you mean virtual threads in JDK 21 could you please elaborate why they are a mistake? I'm not a Java developer but from what I see new concurrency model allows to write much more efficient network software - OS threads are expensive (in terms of RAM/CPU) and to handle many thousands of network connections (C10k problem) you have to either throw a lot of hardware or use NIO (New/Non-blocking I/O) which is hard to code.

One of reasons why Go become popular - gorutines look like normal threads (relatively easy to code) but more efficient.

MichaelMoser123 · 2 years ago
you didn't like java generics? why?
jmyeet · 2 years ago
Examples [1] of these features.

[1]: https://www.happycoders.eu/java/java-22-features/

thisislife2 · 2 years ago
Now this may seem like a silly question, but if I only want to run Java apps on macOS, what should I install from MacPorts? Searching for "JRE" gives no results. Searching for OpenJDK gives various options including:

- OpenJDK21: https://ports.macports.org/port/openjdk21/details/ , - openjdk21-zulu: https://ports.macports.org/port/openjdk21-zulu/details/ - openjdk21-oracle: https://ports.macports.org/port/openjdk21-oracle/details/

each of them having so many variants (see the links). What variant needs to be installed to just run a Java app?

mike_hearn · 2 years ago
Usually Java apps come with Java these days, like how there's no independent way to run Electron apps, it just comes with the download of the app itself. There are tools that make it easy like jpackage and (toots my own horn) https://www.hydraulic.dev

If you want to just quickly run a fat jar then any will work. "openjdk21" is fine. The Zulu variant comes with JavaFX which simplifies things if your app needs that.

thisislife2 · 2 years ago
For sure, Java developers need to use a tool like yours to distribute their app. My experience with some Java applications was that running it triggered an attempt to download (I assume) the JRE, which I cancelled as I didn't know the source of download and didn't want it to download some PUA or malware. (I remember JRE for windows used to bundle some browser toolbar on windows). Hence my attemt to install it myself beforehand. But, since Oracle has stopped distributing latest JRE through Java.com for macOS, I looked for it in MacPorts. And it is really confusing - for example, if you look at the OpenJDK-Zulu port that you recommended - https://ports.macports.org/port/openjdk21-zulu/details/ - it has 4 variants:

1. Applets ( Advertise the JVM capability "Applets".)

2.BundledApp ( Advertise the JVM capability "BundledApp". This is required by some java-based app bundles to recognize and use the JVM.)

3. JNI ( Advertise the JVM capability "JNI". This is required by some java-based app bundles to recognize and use the JVM.)

4. WebStart ( Advertise the JVM capability "WebStart".)

- what the heck? I think I am better off without Java. :)