Skip to main content

Java 14 Features Intro

Banner java icon

πŸŽ‰ Java 14: The Fun Ride of New Features! πŸš€β€‹

Java 14 officially hit the scene on March 17, 2020, bringing a fresh wave of exciting features! Buckle up as we take a joyful ride through some of the 16 new goodies added to our beloved Java programming language. β˜•πŸ˜ƒ

You can grab the JDK 14 binaries here. Now, let’s get started! πŸŽοΈπŸ’¨


1️⃣ JEP 305 – Pattern Matching for instanceof (Preview)​

Ever felt like Java made you write too many boring type checks? Well, Java 14 is here to rescue you! πŸ¦Έβ€β™‚οΈ

Now, instanceof has been upgraded with pattern matching, allowing us to avoid redundant casting. Here’s how it works:

if (obj instanceof String s) {
// No need for explicit casting, s is already a String!
} else {
// Sorry, obj is not a String πŸ˜”
}

βœ… No more manual casting! If obj is a String, Java automatically assigns it to s.

πŸ’‘ But be careful! This only works if obj is not null.

πŸ‘‰ Read More


2️⃣ JEP 368 – Text Blocks (Second Preview)​

Tired of escaping quotes and string concatenation nightmares? πŸ₯΄

Text Blocks to the rescue! πŸ¦Έβ€β™€οΈ Say goodbye to messy multi-line strings and embrace the beauty of triple double-quotes (""").

String textBlock = """
Hello,
World!
Welcome to Java 14 πŸŽ‰
""";

πŸ’‘ Cool Fact: Text blocks behave exactly like String literals, so you can use them anywhere!

String string = "Hello";
String textBlock = """
World""";
System.out.println(string + textBlock); // HelloWorld

πŸ‘‰ Read More


3️⃣ JEP 358 – Helpful NullPointerExceptions πŸ˜±β€‹

Let’s be honest – NullPointerExceptions (NPEs) have haunted every Java developer at some point. 😨 But Java 14 makes debugging them a breeze! πŸŽ‰

Enable this magic by running your program with:

-XX:+ShowCodeDetailsInExceptionMessages

Now, when an NPE strikes, Java will tell you exactly which variable was null! 🎯

public class HelpfulNPEExample {
public static void main(String[] args) {
Employee e = null;
System.out.println(e.getName());
}
}

πŸ”₯ Before: NullPointerException πŸ”₯ Now: Cannot invoke "Employee.getName()" because "e" is null

No more guessing games! 🎯

πŸ‘‰ Read More


4️⃣ JEP 359 – Records (Preview) πŸ“‹β€‹

Java 14 introduces Records, a fancy way of saying: β€œNo more boilerplate code for data classes!” πŸŽ‰

public record EmployeeRecord(Long id, String firstName, String lastName, String email, int age) {}

πŸ’‘ No need to manually write getters, equals(), hashCode(), and toString() – Java generates them all! 🎩✨

πŸ‘‰ Read More


5️⃣ JEP 361 – Switch Expressions (Standard) πŸŽ›οΈβ€‹

The Switch Statement just leveled up! Now it returns values and supports multiple case labels. 🎯

public static Boolean isWeekDay(Day day) {
return switch(day) {
case MON, TUE, WED, THUR, FRI -> {
System.out.println("It is a Weekday!");
yield true;
}
case SAT, SUN -> {
System.out.println("It is the Weekend!");
yield false;
}
};
}

πŸ’‘ No more break statements! And for enums, default cases are optional!

πŸ‘‰ Read More


6️⃣ Other Cool Features πŸ› οΈβ€‹

πŸ”Ή JEP 343 – Packaging Tool (Incubator)​

Create native installers for Java applications (MSI, EXE, DMG, RPM, DEB). πŸ—οΈ

jpackage --name myapp --input lib --main-jar main.jar

πŸ”Ή JEP 345 – NUMA-Aware Memory Allocation for G1​

Better memory management for multi-core processors. ⚑

πŸ”Ή JEP 349 – JFR Event Streaming πŸ“Šβ€‹

Continuous JDK Flight Recorder (JFR) event monitoring.

πŸ”Ή JEP 352 – Non-Volatile Mapped Byte Buffers πŸ’Ύβ€‹

Support for persistent memory in MappedByteBuffer.

πŸ”Ή JEP 363 – Remove CMS Garbage Collector πŸ—‘οΈβ€‹

CMS is gone! Time to embrace G1 or ZGC.

πŸ”Ή JEP 367 – Remove Pack200 API πŸš«β€‹

Pack200 compression is history! Java is getting lighter.

πŸ”Ή JEP 370 – Foreign-Memory Access API (Incubator)​

Safely access non-heap memory (e.g., native or persistent memory). πŸš€


πŸŽ‰ That’s a Wrap! πŸŽ¬β€‹

Java 14 is packed with awesome features to make coding more fun and efficient! Have questions? Drop them in the comments below! πŸ’¬πŸ‘‡

** Happy Coding! πŸš€β˜•**