Meetup: Java 16

Sourcelabs is a company specializing in building software for the JVM, at the moment we mostly use Kotlin at our clients, but we still use and love Java as well. Being a JVM company we feel obligated to keep up to date with all the latest changes in the Java programming language. With the current release of Java 16, we wanted to take a look at all the new and interesting changes in Java 16, but also the upcoming features in Java 17.

We started the day with an informative youtube video by Sander Mak covering the latest changes in Java 16.

For the record

Records were on preview in Java 14 and 15 and have been finalized in this version of the JDK. We wanted to get hands on and implemented a simple Spring Boot application leveraging Jacksons support for Java records as described in this article.

Having worked with Kotlin for the last few years, we could not get past the feeling that Java records feel a bit light on features comparing to Kotlin’s data classes. We found that Java records is lacking some nice features which are available in Kotlin, like the copy function and being able to not exposed private fields, but that might be just a spoiled Kotlin developer speaking.

From a Kotlin point of view, you can also add support for Java records to your existing Kotlin codebase by using the @JvmRecord annotation. This gives your Java clients the possibility to also leverage the copy method. As explained really well in the following video:

Finally, I still do not know how I feel about a record not being a class. Where a Kotlin data class is still a class, a record is a record – a different thing like interface.

Sealed classes

We proceeded looking at sealed classes and their workings. Introduced in Java 15 as a preview option, now also released as a final implementation. Sealed classes are a nice to created a restricted type hierarchy and in case you ever need to build a state machine they are the way to go. By making an abstract class defining the parent object stating what extensions you allow and thereby sealing of further inheritance can be a really nice feature if you have the use case. If you want to know more about them check this article.

Pattern matching

Don’t you just hate to cast a variable after you just checked it to be an instance of something? Well in Java 16 they made it easier, not as easy as Kotlin because you have to assign it into a new variable instead of just using the current one. At least it’s less typing than it was before. See the documentation of Oracle for the details.

Conclusion

All in all, it was nice to play around with the new features of Java. Being a spoiled Kotlin developer, for me the new features introduced in Java wont be the reason to make me go back to Java, but at least it is good to see that new features are continued to be added to the ‘old’ Java programming language. Even though the changes seem a lot of times ‘just not as cool as Kotlin did it’, at least developers restricted to using Java are getting an improved programming experience.

As usual we ended our meetup with a nice borrelbox that included some tasty drinks and snacks!

Did you enjoy this content? We have got more on the way! Sign up here for regular updates!

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *