Java 17

  1. Text Blocks

  2. Improved Switch Statements

  3. Record Classes

  4. Sealed Classes

  5. Pattern Matching with instance of

  6. Helpful Null pointer Exception

1.Switch Enhancement:

We use case label and arrow symbol instead of case and also, we pass the object to switch statement (pattern matching in java 17)

Pattern Marching switch increase the performance of 0(1) if we write if else then O(n).

2. Record Class:

DataCarieerClass (a class which hold data only).

record class is to simply or concise the code and it has private and final field which are immutable and also, we can create constructor inside the record and this record class will implements but not extends. we can create static variable, concrete methods but not instance variables;

ex: record Employee(int id, String name) {}

record Employee(int id, String name) {

public record(int id, String name) {

this.id=id;

}

}

3.Sealed Class:

its an interesting feature it gives the permission that which class can extends my class.

when sealed class gives permits then it sub class should be in final or sealed or non sealed(any class can extend this class if it has non sealed)

class A extends Thread permits B, C {

}

final class B extends A {

}

non sealed class C extends A {

}

4.Text Blocks:

To maintain the String structure we use (“““ structure “““) so that it will print as the same struture we given in the Structure in stead of using like \n this.

5.Helpful Null pointer Exception:

In Java 17, one of the enhancements introduced is the Helpful NullPointerException. This feature provides more informative error messages when a NullPointerException occurs, making it easier for developers to diagnose and fix the underlying issue.

What is a NullPointerException?

A NullPointerException occurs in Java when you attempt to perform an operation on an object that is null. This can happen in various situations, such as:

  • Calling a method on a null object.

  • Accessing a field of a null object.

  • Attempting to use an array or collection that is null.

How Helpful NullPointerException Works

With the Helpful NullPointerException, Java 17 provides detailed messages that indicate which variable was null, helping developers quickly identify the source of the problem.

Example

Here’s an example demonstrating how this feature works:

Before Java 17

Consider the following code that might produce a NullPointerException:

javaCopy codepublic class Main {
    public static void main(String[] args) {
        String str = null;
        System.out.println(str.length()); // This will throw NullPointerException
    }
}

When running this code in Java 16 or earlier, you would get a generic NullPointerException message:

cssCopy codeException in thread "main" java.lang.NullPointerException
    at Main.main(Main.java:4)

In Java 17 with Helpful NullPointerException

Now, if you run the same code in Java 17, the output will be more informative:

phpCopy codeException in thread "main" java.lang.NullPointerException: Cannot read the length of "str" because "str" is null
    at Main.main(Main.java:4)

6.Pattern Matching with instance of:

instanceof is used to check the type is inherited or not.