r/java • u/daviddel • Mar 03 '20
Java Magazine: Java 14 Arrives with a Host of New Features
https://blogs.oracle.com/javamagazine/java-14-arrives-with-a-host-of-new-features48
u/petrefax Mar 03 '20
Some good stuff there. The NPE change in particular is amazing.
18
u/lapa98 Mar 03 '20
My god is that npe change something I felt was needed for a long time
19
u/macuserx Mar 03 '20
I've begged, plied and cried for this change ever since Java 1.4. Never did they listen. Until now. Maybe it's a coincidence that I cried for it in Java 1.4 and getting it in Java 14 :)
37
2
1
u/nlisker Mar 04 '20
Never did they listen.
And in actuality, that change was not made by Oracle, but by an outside contributor.
2
5
u/pure_x01 Mar 03 '20
Many new languages and other updated languages like C# go great length of avoiding NPE's as much as possible. It would be nice if java followed that path.
3
u/Polygnom Mar 04 '20
Well, with sealed interfaces and records it will slowly become possible.
In Haskell, you get the Maybe Monad like
type Maybe x = Just x | Nothing.If they retrofit Optional in Java to something like:
sealed interface Optional<T> permits Just<T>, Nothing {}; record Just<T>(T value) implements Optional<T> { Just(T value) { if(value == null) throw new IllegalArgumentException("Just can not store null values, use Nothing instead"); } } record Nothing() implements Optional {};Then you could truly start with null-free programming.
var x = map.get("foo"); switch(x) { case Nothing -> ...; case Just(T value) -> ...; }What I would really want some day is pattern-matching in method dispatch (and method parameters).
Imagine you have the above Optional implementation and an interface like this:
public interface Foo { public boolean bar(Optional<Widget> x); }And the compiler would allow this kind of implementation for it:
public class FooBar implements Foo { public boolean bar(Nothing x) { ...; } public boolean bar(Just(Widget x)) { return x.frobnicate(); } }Ah, one can dream....
1
Mar 04 '20
The pattern matching in case statements you showed brings a large part of the benefits already. AFAIK pattern matching is still a preview feat.
2
u/Polygnom Mar 04 '20
Records are in preview as well, and sealed types are currently expected to be previewed in 15 (? I think).
This was about future paths for Java, so obviously I was talking about preview features and hypothetical features.
7
u/sievebrain Mar 03 '20
There's kotlin for that. I'm not sure how easy it would be to integrate nullable types into Java in a backwards compatible way - that sounds hard to do without making the syntax really ugly.
4
u/thomascgalvin Mar 03 '20
If maintaining backwards compatibility is a necessity, avoiding deafault-nullable types is just about impossible.
They could add something like
String! foo = "bar"to indicate that a variable isn't eligible for nullity, but that doesn't fix the same issues.And if you go that route, we've already got the
@NotNullannotation.13
u/pron98 Mar 04 '20
avoiding deafault-nullable types is just about impossible
Adding generics was a bigger challenge. The issue here is that there's no clear winner among several approaches yet, and the magnitude of the problem is unclear.
2
u/Polygnom Mar 04 '20
avoiding deafault-nullable types is just about impossible
In principle, you are right. retro-fitting it into a language tht has evolved for so long is difficult, though. Sure, if you were to develop a completely new language this would be rather straight-forward. But making it happen inside Java & the JVM without breaking existing, not null-free code? That is another story.
4
u/pron98 Mar 04 '20
Adding generics was harder.
1
u/Polygnom Mar 04 '20
What makes you think that?
Erased generics meant that the API didn't need to change. I don't see that possible as easily for the whole JDK API with non-nullable types.
if you are having the stroke of genius idea how to solve that, I'd love to hear that.
3
u/pron98 Mar 04 '20
The JDK APIs aren't a fundamental issue other than just requiring a lot of annotation work. Return types can be narrowed to non-nullable types where appropriate, and non-nullable arguments would throw, as they do today, when called from legacy code. There are various questions about inference and the best way to compile old code with a new type system (e.g. raw types after generics), but the problems are surmountable. What isn't clear is how big of an effort this issue justifies, and which of the many possible approaches is the best one. There are sound approaches being tried in Java, as well as less sound ones explored by various static analysis libraries and IDEs.
2
u/dpash Mar 04 '20
You can go some of the way by using the various
@NotNull/@Nullable/@NonNullApiannotations around your code and using something like ErrorProne to enforce that during compile.Returning
Optionaland usingObjects.requireNonNull()can also help prevent them from happening so often.
30
u/Hangman4358 Mar 03 '20
Records, the NPE changes and the instanceof changes are my top favorites. Too bad it will be another 15 years until my team can switch to anything past 8 :'(
19
u/BoyRobot777 Mar 03 '20
In my big corporation we recently switched to staying on latest Java version. Very motivating change :)
11
2
u/Hangman4358 Mar 04 '20
Since we deal with PHI and have to be HIPA complient, the entire corporate structure didn't start it's glacial shift to the cloud until late 2018.
But once we are in a position where our components are running in containers in the cloud and we can incorporate a jvm into our build artifacts instead of relying on some data center team half way around the world to update thousands of VMs with a new version I am hopeful we can move to be cutting edge too.
1
u/s888marks Mar 03 '20
Wow, really the latest, as in you'll move from 13 to 14 soon?
6
u/BoyRobot777 Mar 04 '20
soonish. We do canary release firstly with new version. For 3 months we compile to new version, but use previous version features. Once everything is good, we move to new version. But yeah, its not years for new Java version. Also, it took some time convincing our architects.
1
4
Mar 04 '20
Yeah, I just completed an upgrade to 8 at my client. They where still running some modules in 1.5!
5
u/CrommVardek Mar 04 '20
Our team recently inherits an old Java application. A nice middle-tier. Running on plain Java 1.2 ... On a 2003 single-thread windows server. They tried to run the application on a docker image of the same server but it failed because the image ran on a multi-thread machine. If I didn't have to maintain this application I would think it was a joke. Sadly it is not. And of course it is undocumented, uses a xml "template" to connect to databases and run procedures. End my developer life please.
3
1
u/boobsbr Mar 04 '20
My team is still on Java 6. I've heard from people in this sub they're still on 5.
9
u/TM254 Mar 03 '20
I miss the PDF versions, anyway someone can get them? Internet is not cheap in 3rd world countries 😂
10
2
u/neutronbob Mar 04 '20
Every Java Magazine article has a link to a PDF version of it in the upper right corner.
1
u/TM254 Mar 04 '20
There used to be a single pdf for all articles, what's happend? It used to be a real magazine.
4
u/Sedu Mar 03 '20
Can’t wait for the openjdk release this 17th! I’m already building using the latest jpackage utility in the previews but can’t release until I’m actually using openjdk obviously.
The pattern matching instanceof is particularly nice.
4
6
u/BlueGoliath Mar 03 '20
No mention of Panama. You could almost play a drinking game based on the amount of articles that don't even mention it.
10
u/cl4es Mar 03 '20
At least you're getting a piece of panama as an incubating feature: https://openjdk.java.net/jeps/370
-1
u/sievebrain Mar 03 '20
Yeah I'm really curious what went wrong there. Seems they got distracted by vector types for a couple of years, but even so, nothing delivered after so long? It's an ffi, not something that's never been done before. Valhalla I understand why it's slow.
9
u/BlueGoliath Mar 03 '20
Not sure about the history of Project Panama but they've been trying to balance a lot of things and try to make everyone happy... which isn't easy.
They had an easy-to-use Pointer API for awhile but then realized that it was too restrictive and wouldn't work in a lot of situations. Now there is a MemoryAccess API which has a foreign part layered on top. Only the MemoryAccess part is being delivered right now. The new MemoryAccess API isn't fun to use without an abstraction layer though.
TL;DR, shits hard to get right.
5
u/coder111 Mar 03 '20
So I take project Loom is not finished in time to release with version 14? Some more waiting is required to get fibers?
11
u/BoyRobot777 Mar 03 '20
I think they have some way to go to stabalize it. Just recently it was an issue with Loom and million virtual threads. No idea how serious it is.
Plus, they recently started thinking about
select, something similar that golang has.Lastly, I think structured concurrency is still not there.
My hope is these things will come incrementally and not as one big change. It would be cool to have just virtual threads, before structured concurrency, and other nice things. So I would guess Java 16-17 for plain virtual threads.
2
u/pure_x01 Mar 03 '20
AWS lambda will probably be stuck at 11 for a couple of years. No production grade custom runtime exist afaik
1
u/DrunkensteinsMonster Mar 04 '20
Is java really the best tool for a Lambda anyway? Aren’t start times an issue there?
1
2
2
u/vikarjramun Mar 04 '20
Why doesn't pattern matching use intelligent scoping like Kotlin?
Wouldn't it be nice if
if (o instanceof String) {
//o is a String here
String s2 = o.toUpperCase();
}
//o is an object here againworked?
2
u/oldGrumpyCodeMonkey Mar 05 '20
Not sure how Kotlin handles this but I asked the same thing on one of the jdk dev lists and the response was that if o is a field that could be set from another thread then it be set to a non string after being tested but before being used.
5
Mar 03 '20 edited Mar 03 '20
Nice to see a lot of quality of life changes. Java’s biggest problem for me has always been the syntax and verbosity of the code. Love to see them try to address some of these issues.
10
2
u/rusticarchon Mar 04 '20
Guess Lombok is pretty dead with that new 'record' class type?
1
u/laviksa Mar 06 '20
Not at all. Does a record have getters and setters? Can new values be assigned to record types? Nope, so you can't use it to update database entities (jpa or hibernate for example). It's really a missed oppurtunity IMO.
1
u/LazyAAA Mar 03 '20
These text blocks are so ... fine (and all the complex parsing associated with these)
String text = """Lorem ipsum dolor sit amet, consectetur adipiscing \elit, sed do eiusmod tempor incididunt ut labore \et dolore magna aliqua.\""";
(Being quite lazy person) I can see my IDE refactoring existing "text blocks" with all the "beautiful" concatenations into new Java 14 syntax - Nirvana :)
1
u/nutrecht Mar 04 '20
So cool! It's almost like Java 8 in the sense that there's a ton of stuff people will really want to use.
1
Mar 03 '20
The only thing that make me sad is the stupid three quotes for text block.
2
u/s888marks Mar 06 '20
An earlier proposal (Raw String Literals) used backquotes. Everybody hated it and said we should use triple double quotes like other languages.
1
Mar 07 '20
How they hated? It makes no sense for me, it seems just ugly and verbose, but wathever, if the community decided, I just can try to adapt.
0
u/VM_Unix Mar 04 '20 edited Mar 05 '20
Agreed. Perhaps they should have taken inspiration from C# here.
https://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp
0
u/VM_Unix Mar 05 '20
Were they borrowing from Kotlin for the multi-line string syntax?
https://realkotlin.com/tutorials/2018-06-26-multiline-string-literals-in-kotlin/
0
Mar 04 '20
[deleted]
1
u/daviddel Mar 05 '20
If all the new features are being back ported to 8, what would be the point of newer releases? Incubating sand-boxes for JDK 8 "new" features? ;-)
FWIW, this feature has been contributed by SAP and is basically coming from their commercial JVM which has that capability since 2006. This FOSDEM sessions gives more background https://fosdem.org/2020/schedule/event/npes/
0
Mar 03 '20
[deleted]
5
1
u/DannyB2 Mar 06 '20
I think you don't have to use the new features.
Unlike Python 2 to 3, running your old code on newer JVM (even newer compiler) should not break.
9
u/8igg7e5 Mar 03 '20
Reminder, almost all of the features people are excited about are Preview features.
If you're a commercial developer you can't (really really shouldn't) use these in production yet.
Production Features
349: JFR Event Streaming
358: Helpful NullPointerExceptions
361: Switch Expressions (Standard)
364: ZGC on macOS
365: ZGC on Windows
Preview Features
305: Pattern Matching for instanceof (Preview)
359: Records (Preview)
368: Text Blocks (Second Preview)
I expect Text Blocks will be in Java 15 and looking at feedback I wonder if Pattern Matching for instanceof might as well.
Records is already planned to have another preview in Java 15, and with time for feedback that might mean it misses Java 16. Java 17 is the next Oracle LTS so let's hope it makes it by then as I suspect we'll see adoption bumps around the LTS versions (justifiably or not).