Java 15 features every Java developer should know

Java 15 just arrived.

There were in total 14 features introduced in this release.

But not every feature needs to be understood from a developer perspective.

Only 4 of the 14 features are must to know features for java developers to utilize in their java applications.

Here are those four:

  1. Sealed classes
  2. Text blocks
  3. Records
  4. Pattern matching for instanceof

Of these only ‘Records’ are available as a permanent feature. The rest three are preview features. So to use them you need to compile the classes with the preview feature flag enabled.

Let’s look into each of them in detail.

Sealed classes:

Ever thought that you want only certain classes to extend a class you created?

Is there a way to block other classes from extending your class?

And if you could do so how to make the extended class propagate this “sealing”.

This can be achieved through “Sealed Classes”.

You seal a class and permit only certain classes to extend them like this :

package sealed;
 
public sealed class Animal permits Cat,Dog{
 
 
 
}

Animal is a sealed class which allows only Cat and Dog classes to extend it.

The compiler will complain if any other class tries to extend Animal class.

Also , in turn Cat and Dog classes should specify whether or which classes can extend them.

Know more about this here :

Text blocks

Say you want to write long sentences and assign them to a String variable in java.

The sentences have many newline characters.

And representing the sentences in the same line is too lengthy so you break it down across many lines by splitting them and concatenating them.

For example , to represent the below text:

I am a full stack developer.
I work on "HTML", "CSS" , "Javascript" and "Java" technologies.
Today I am writing about Java 15 features.

You need to create a string variable and assign the text like this :

 String introduction = "I am a full stack developer.\n"
			+ "I work on \"HTML\", \"CSS\" , \"Javascript\" and \"Java\"" + " technologies.\n"
			+ "Today I am writing about Java 15 features";

Notice that for every new line you use the \n character.

And for splitting the sentences across multiple lines in the source code so that it is readable you split the sentences into small sentences and concatenate them using “+” operator.

And to include double quotes you escape them using backslash character \.

Text blocks removes the need for all of these.

You can represent the above text like below using text block:

String introduction = """
			I am a full stack developer.
			I work on "HTML", "CSS" , "Javascript" and "Java" technologies.
			Today I am writing about Java 15 features.
			""";

Notice that there is no concatenation operation , no backslash symbols and no newlines.

The way you write is the way it gets assigned to the string variable.

To implement this , follow the below steps:

  • Use three quotes at the beginning of the sentences “””.
  • Press enter (you need to write the sentences in the next line and not next to the triple quotes)
  • Write the sentences you want the way you want it (If a new sentence begins just include them in the next line – no need to include \n character)
  • Close the sentences using another triple quote

Notice that the closing triple quote can either be appended immediately to the end of the sentences or it can be appended in the next line but inline with the other sentences.

If the ending triple quote is behind the other sentences then the trailing white spaces will be considered as content.

For example in the below text




 String introduction = """
			I am a full stack developer.
			I work on "HTML", "CSS" , "Javascript" and "Java" technologies.
			Today I am writing about Java 15 features.
		""";

The end triple quote is behind the other sentences. So all the white space behind the other sentences are considered as text content.

So the output will be :

Notice the trailing white spaces which got included because of the way the end triple quote was placed.

You can include newline characters and other special characters inside a text block the same way you include it in a regular string literal.

For example :

String introduction = """
			I am a full stack developer.\n
			I work on \"HTML\", "CSS" , "Javascript" and "Java" technologies.
			Today I am writing about Java 15 features.
			""";

In the above text block , I have added a newline character and have represented single quotes using backslash character for the word “HTML”. Both are treated the same way as they would be for regular string literals. A new line gets printed after the first sentence and HTML is enclosed within quotes as shown below:

Records:

Records were introduced in Java 14 as first preview and now it is introduced as a second preview feature.

Records eliminate the need to write manual getters and setters, equals method , hashCode method and toString method for Java Bean classes.

For example below is a Record class:

public record ShoppingOrderRecord(String name,int quantity,float price) {
 
}

It is similar to a regular class except that the getters and setters , equals(), hashCode() and toString() methods are automatically created.

You can instantiate a new instance just like as for a regular class:

ShoppingOrderRecord order = new ShoppingOrderRecord("Item A", 100, 100.50f);

More about it here

Pattern Matching for instanceof

Let’s say you have a generic method which accepts any object as a parameter.

Based on the type of the object , the method performs custom operations.

Traditionally to implement this you need to do the following steps:

  1. Check the passed parameter using instanceof operator for the specific type
  2. If it matches , cast the passed parameter to the specific type and assign it to a variable.
  3. Perform the custom operation on the new variable

Using pattern matching for instance of , you can get rid of the second step.

Let’s check it using an example.

Lets create a generic method add() which takes a single parameter.

If it is an integer it adds the number to itself and returns it.

If it is a string it concatenates the value to itself and return it.

Here is the code written in the traditional way:

 Object add(Object obj) {

		if (obj instanceof Integer) {

			Integer value = (Integer) obj;

			return value + value;
		}

		if (obj instanceof String) {

			String value = (String) obj;

			return value + value;
		}
		return null;

	}

You can refactor the above code using pattern matching like this:

Object addWithPatternMatching(Object obj) {

		if (obj instanceof Integer value) {

			return value + value;
		}

		if (obj instanceof String value) {

			return value + value;
		}
		return null;

	}

Both return the same output but the latter has less lines of code.

In the latter case , the variable to be cast is immediately declared next to the instanceof check of the parameter passed. It is then used within the scope.

That’s it!

All the other features are mostly used to improve the internal functionality of Java.

Those can be referred here:

https://openjdk.java.net/projects/jdk/15/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s