Hey Tea Lovers. On 22 March 2022, JDK 18 or Java 18 is released to the public (from Oracle). Java 18 does not contain any major updates that will change how we code, but it does have something very interesting. Few of the updates are in the incubator or preview mode. So for this article, I will be mostly focused on the core library addition. Don’t worry I will add a summary of other updates as well.
I would be happy to connect with you guys on social media. It’s @coderstea on Twitter, Linkedin, Facebook, Instagram, and YouTube.
Please Subscribe to the newsletter to know about the latest posts from CodersTea.
Before Jumping into Java 18
A Big Thank You to the contributing team of Java to work this hard on the platform.
As you must have known that Java has adopted a 6-month release cycle. This means it will release a new version of Java every 6 months. And after every 3 years, an LTS version will be released i.e. every 6th version. Currently, Java 17 is the LTS version, released in September 2021. So Java 18 would only have short-term support of 6 months.
If you are curious about the previous versions, here is my short update on what are the new features added in previous Java releases.
Java 17
Java 17: These 5 New Features Make Java 17 Special
Java 16
Java 16 Record to Reduce Boilerplate Code of POJO
Java 16 is Here! New Features of Java 16 Simplified
Java 11
The Things I Love and Use from Java 11
Ok, without any further ado, let’s look at Java 18. But before, prepare your cup of tea to sip, code, and learn.
A Simple Web Server without any Code in Java 18
Yes, you read it right. Java 18 now can bring up a simple static web server in an instant. No need to rely on other scripting languages or write a whole bunch of code just to generate a static web server.
The command jwebserver
can convert the given folder into a simple HTTP file server in an instant. Simply cd
into the older you want to make a static page server, run the command jwebserver
, and hit enter. By default, the port would be 8000
on localhost
. Just visit the localhost:8000
and now you can see the static website.
You can change the port with the help of -p
an option or can specify a directory with -d
or --directory
.
You can learn more about it on the official release info on JEP 408: Simple Web Server.
Introducing @snippet
Java 18 has introduced a @snippet
tag to add a code snippet in Java Doc. Now you don’t have to add a snippet <pre>{@code ...}</pre>
and are not able to see properly formatted code snippets. Here is the basic usage of @syntax
tag in Java Doc.
/**
* The following code shows how to use {@code Optional.isPresent}:
* {@snippet :
* if (v.isPresent()) {
* System.out.println("v: " + v.get());
* } else {
* System.out.println("v's value is NULL");
* }
* }
*/
Code language: PHP (php)
With the @snippet
not only you can add inline code snippets, but also be able to add external file content. And there are much more options, which you can check out on JEP 413: Code Snippets in Java API Documentation
UTF, UTF Everywhere
Starting Java 18, Java will be setting its default charset as UTF-8
. The motivation behind this is to have a consistent result on various platforms, operating systems, locales, and configurations for the APIs that depend on the default charset.
Here is an example from the official document where a Japanese text file encoded in UTF-8
on macOS is corrupted when read on Windows in US-English or Japanese locales. Here is how it behaves.
java.io.FileReader(“hello.txt”) -> “こんにちは” (macOS)
java.io.FileReader(“hello.txt”) -> “ã?“ã‚“ã?«ã?¡ã? ” (Windows (en-US))
java.io.FileReader(“hello.txt”) -> “縺ォ縺。縺ッ” (Windows (ja-JP)
Code language: CSS (css)
As it’s a default behavior we don’t have to change anything ourselves. In case you want to use different charset, you can do so with -Dfile.encoding
.
Bye Bye finalize()
Java 18 has deprecated the finalization for removal i.e your @finalize()
method. So in the future release, the finalization won’t be active. What is finalization you say? Simply put, it’s a way of cleaning objects that Garbage collectors couldn’t reach. It’s a feature that is utilized by users. More information can be found on the official Oracle Doc. But the finalization has its consequences such as resource leaks.
The in-depth reasoning and a well-documented example can be found on JEP 421: Deprecate Finalization for Removal. I highly recommend you guys read it. Also if you are not familiar with the working of Garbage Collector please read through Get Ready to Deep Dive in Java Memory Management: Garbage Collector
The Reimplementation of Core Reflection
Before talking about this topic, let me ask you if you know about Reflection API in Java. If not, I highly recommend you to read my simple and easy-to-understand post on Java Reflection API: The Magical World of Java.
The JEP 416, hasn’t added or removed any methods, it simply reimplemented them. The reason behind this is that there were various ways it handled the reflection to handle better peak performances. Now when there were any changes to underlying methods, the Java team needed to change a lot of things. With this implementation, the maintainability and the cost of development are reduced.
This change is not something you will notice, or observe in your day-to-day coding. But I just wanted to highlight the fact that when building something you not only have to see if it is working but also how it affects the future maintainability and development cost.
The Previews, Incubators, and the Future of Java
There are other changes but a few of them are in incubators and preview mode. But anyhow the constant change and upgrades to Java are surely making Java a good fit even in these changing times. Following are the other changes that I haven’t discussed in detail but will also make the next release amazing.
Conclusion
That’s it for this post. Hope you liked it. Please let me know in the comments recommendation for the next post’s topics.
See you in the next post. HAKUNA MATATA!!!
I would be happy to connect with you guys on social media. It’s @coderstea on Twitter, Linkedin, Facebook, Instagram, and YouTube.
Please Subscribe to the newsletter to know about the latest posts from CodersTea.