Java 23 Is Around the Corner: What You Need to Know
Java 23 is complete and will be available to everyone on September 17th. Let's take a look at some of the new features and improvements this version brings to the language.
Features
Before we dive into the features, it's important to note that Java 23 is not an LTS (Long-Term Support) version. This means vendors like Oracle and Microsoft won’t provide long-term support for it. The next LTS version will be Java 25, set to be released next year.
Still, staying updated with the new features now will help us take advantage of them when the next LTS is released.
This version has 12 JEPs (JDK Enhancement Proposal) 🤯
455:Primitive Types in Patterns, instanceof, and switch (Preview)
471:Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal
477:Implicitly Declared Classes and Instance Main Methods (Third Preview)
Many of these JEPs are still in preview, which means that they can evolve and maybe have break changes.
Let’s highlight some of them that are more related to developer activity.
JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview)
This feature follows the principles of instanceof pattern, allowing the developers to use the pattern matching with primitive types.
For example:
switch (x.getYearlyFlights()) {
case 0 -> ...;
case 1 -> ...;
case 2 -> issueDiscount();
case int i when i >= 100 -> issueGoldCard();
case int i -> ... appropriate action when i > 2 && i < 100 ...
}
If you're not familiar with pattern matching yet, I recommend checking out this article:
JEP 477: Implicitly Declared Classes and Instance Main Methods (Third Preview)
This is a great feature for teachers and newcomers to the Java world. This JEP makes it easier to start an application in Java by allowing you to run it without needing to add static
, class
, or arguments
. It simplifies things, especially for beginners.
void main() {
println("Hello, World!");
}
JEP 482: Flexible Constructor Bodies (Second Preview)
This feature allows the developers to add some code, before super in the constructor.
Instead of having something like this:
public class PositiveBigInteger extends BigInteger {
public PositiveBigInteger(long value) {
super(value); // Potentially unnecessary work
if (value <= 0) throw new IllegalArgumentException(..);
}
}
You can do that:
public class PositiveBigInteger extends BigInteger {
public PositiveBigInteger(long value) {
if (value <= 0) throw new IllegalArgumentException(..);
super(value);
}
}
JEP 476: Module Import Declarations (Preview)
I like this feature, this will avoid the fight between check styles, of using multiple type-import vs import with wildcard (*
)
These are the main goals of this feature:
Simplify the reuse of modular libraries by allowing entire modules to be imported at once.
Avoid the noise of multiple type-import-on-demand declarations (e.g.,
import com.foo.bar.*
) when using diverse parts of the API exported by a module.Allow beginners to more easily use third-party libraries and fundamental Java classes without having to learn where they are located in a package hierarchy.
Do not require developers who use the module import feature to modularize their own code.
I like using imports because they help spot violations of the Single Responsibility Principle. However, I'm not too fond of the debate between using multiple specific imports and using wildcard (*
) imports.
This feature is still in preview and could change significantly. What are your thoughts about this feature?
Last week on The Out of the Box Developer, my friends and I discussed some exciting news about Java 23, with a special guest, Bazlur, who shared many key features that can benefit you as a developer. Feel free to check it out later!
Conclusion
Java has been evolving a lot, and keeping updated can help us to be more productive and reduce bugs. These 4 JEPs we’ve seen belong to Project Amber, whose main goal is to enhance developer productivity.
I hope this article helped you understand the latest news about Java. If you want to stay updated, don’t forget to subscribe!