Here are 11 improvements you get when updating to #Java16 later today: from records and type patterns to `Stream`-additions and Unix domain sockets, from creating installers to more performance and observability. 🧵👇🏾
Express in a single line that a type is just a collection of data without need for encapsulation and let the compiler do the rest:
record Range(int low, int high) { }
That results in almost the same API as the attached class. *drops mic*
#2 Type Pattern Matching
This is actually two-for-one:
* first step into pattern matching
* type patterns with `instanceof`
With a type pattern you check whether a variable is of a certain type and, if so, create a new variable of that type, so you can use it without casting.
#3 Sealed Classes (Preview)
Want to express that a type can be extended not by all types (as per default) or none (final) but just a select few? With sealed types you can do just that!
Beyond expressing intend, this will come in real handy when we get type patterns in switch.
#4 `Stream` Improvements
Is `flatMap` too slow or doesn't work with a pseudo-collection? Use `mapMulti` instead:
// plus wildcards
<R> Stream<R> mapMulti(BiConsumer<T, Consumer<R>> mapper)
If you have an `HttpRequest` and want to create a similar one, you can now clone it to a new builder with an overload for `newBuilder`.
If you have several `BodyPublisher`s whose output you want to concatenate, `BodyPublishers::concat` is there for you.
#6 Unix Domain Sockets
The `ServerSocketChannel` and `SocketChannel` classes can now use Unix domain sockets, which are faster and safer (yet limited to connections on the same host). This works on Linux, Mac, and Windows 10 / Windows Server 2019.
#7 Foreign Linker and Foreign-Memory Access (Preview)
I'll just quote the JEPs:
"Replace JNI with a superior pure-Java development model."
"A single API should be able to operate on various kinds of foreign memory (e.g., native memory, persistent memory, managed heap memory)."
#8 Vector API (Preview)
Another Project Panama incubator, this one unlocks SIMD programming in Java for more performance:
"vector computations that reliably compile at runtime to optimal vector hardware instructions on supported CPU architectures"
It is now possible to stream JFR events over JMX. A remote streaming connection from the server (running the app) to the client (running the JFR tool) writes the same kind of file as an app on the client would, so existing JFR tools require little change.
#10 Packaging Tool
With jpackage, you can create platform-specific packages that can then be installed as is common for each OS:
That's a guiding light of #ProjectAmber and record serialization is the first step. The Inside Java Podcast episode on that topic with Julia Boes and @chegar999 (inside.java/2021/03/08/pod…) gives great insight into how it achieves that. 🧵
1/10
First, what's wrong with regular serialization? In short:
* extralinguistic mechanism (aka "magic")
* undermines encapsulation
* causes security issues
* hinders evolving code
* holds back new Java features
In long (and why it turned out that way), see attached thread.
Do you dream of "value types" in Java? So do I! Hence I was thrilled to see that Project Valhalla is slowly coming out of exploration with two draft JEPs.
Here's what they currently propose. 🧵👇🏾
(If you prefer video: https:/www.youtube.com/watch?v=WBvTilbh8S0&t=344s)
1/10
First, why value types? To bridge the divide between
* primitives (fast, no memory overhead) and
* reference types (abstraction, safety, identity)
As is, we sometimes have to choose between performance and design. And we often choose wrongly.
"When people ask me what feature do I most regret, serialization is the easy answer" - @BrianGoetz
Let's talk about serialization in Java: Why does it exist? What are the problems (even today)? Would Java have been better off without it? Can it be fixed?
1/11
NB: What follows is mostly quotes or paraphrasing from a conversation I had with Brian during my 25-hour live stream. If you want to watch the full discussion about serialization, nullability, primitives, and more, you can find it here:
2/11
Serialization was introduced because turning an object graph into bytes is valuable: You can store things on disk, in databases, or send them over the wire. But while the concept is sound, it was implemented in a horrible way.
Project Amber is making progress on pattern matching in #JavaNext. Here are three recent developments that I'm very excited about and I think you will be as well.
(Caveats: these are ongoing discussions; none of this is final; speculation and strawman syntax ahead)
1. "Array patterns" allow matching and destructuring arrays. The `if`-line does three things:
a) is `objects` a `String` array with length 2?
b) if so, cast it to `String[]` and extract the two elements
c) declare `a` and `b` and assign the two elements to them
"Can I also bind the arrays as a whole?"
Likely. This is called an "as pattern".
"What if the array can have more elements?"
Allowing to express "at least two elements" is being considered - for example with three dots.
Yesterday evening, we spent about two hours digging through the German #Corona app and I'm thoroughly impressed. This is a modern project, developed out there as free software (APL 2.0) on GitHub, and it has stellar documentation.
Android and iOS apps use Google's/Apple's Exposure Notification Framework (ENF; google.com/covid19/exposu…, apple.com/covid19/contac…). Android app is written in Kotlin, iOS app in Swift. (Can't tell you much more because I'm clueless about mobile.)
The electoral swing analysis ("Wählerwanderung") of yesterday's state elections in Germany (e.g. @diezeit, zeit.de/politik/deutsc…) is pretty interesting and I want to comment on who switched to the radical right-wing #AfD.
🖗 Thread
All numbers are relative swing from [voters of $party in 2014 who voted in 2019] to [voters of AfD in 2019] for Sachsen / Brandenburg, i.e. "the usual numbers" normalized by 2019 voters.
In order of their share lost to #AfD, here are the big parties and then former non-voters...
It's pretty obvious that there's no arguing with extremists. Retaining ~90% of voters from one election to the next is "impressive", even for the traditionally stubborn right part of the political spectrum.