What are the new features in Java 14 that a day to day Java developer should know?

There are about 16 changes introduced in Java 14.

Not all of them need to be known for a day to day java programmer to improve his programming.

Here are the 5 features which every Java developer should know which will save his time and improve code quality:

  1. Text Blocks

If you want to define multiple lines using a single string variable you have to concatenate those lines with newline character (“\n”) prior to Java 14.

Java 14 has simplified that using three double quotes “”” at the start and end of the lines as shown in below example in the first string:

public class TextBlocks {

	public static void main(String a[]) {

		String text = """

				This is an example of how to use text blocks.
				No need of using newline characters anymore.
				Java 14 has simplified defining multiline strings.
				""";

		System.out.println(text);

		// To print in a single line
		// But defining the sentences in multiple lines use backslash character.

		String text2 = """

				This is an example of how to use text blocks.\
				No need of using newline characters anymore.\
				Java 14 has simplified defining multiline strings.
				""";

		System.out.println(text2);

	}

}

If multiple lines as defined using “”” characters need to be printed in a single line include backslash character at the end of each line . The output of the above program is :


This is an example of how to use text blocks.
No need of using newline characters anymore.
Java 14 has simplified defining multiline strings.



This is an example of how to use text blocks.No need of using newline characters anymore.Java 14 has simplified defining multiline strings.

Text blocks are still a preview feature and so not advisable to use in applications to be deployed to production environment.

2. Records:

Writing Java code can be cumbersome. There are often times you have to deal with boiler plate code. Particularly with simple POJO classes used just for the purpose of carrying data. Libraries like Lombok help in generating getters and setters , equals and hashcode automatically. Now Java 14 removes that dependency.

Just define a class as record and include the fields within open paranthesis seperated by commas like this :

record RecordData(String name,String emailId) {

}

Java will automatically generate the getters and setters, equals and hashcode methods. For example executing the below code :

record RecordData(String name,String emailId) {

}


public class RecordSample{
	
	
	public static void main(String a[]) {
		
		
		RecordData rec = new RecordData("John","john@gmail.com");
		
		System.out.println(rec);
	}
}


returns the result :

RecordData[name=John, emailId=john@gmail.com]

Records are still in preview feature too.

3. Switch Expressions:

This is explained here in detail :

The evolution of switch expressions

Switch expressions have become a standard feature in Java 14 and hence can be used in production applications.

4. Null pointer exceptions with more details:

Consider the below code:

teacher.getStudents().get(0).getMarks();

if the above code throws null pointer exception Java 14 will show which of the above variables is null (teacher or students or marks)

Prior to Java 14 this was not shown. Only the line number where the exception is thrown was indicated.

5. Pattern Matching for instanceof operator:

Often to avoid ClassCastException java developers use code as similar below :

if(obj instanceof Employee){

Employee e = (Employee)obj;

System.out.println(e.getName());

}

Java 14 simplifies the above code as below:

if(obj instanceof Employee e){

  System.out.println(e.getName());
}

This reduces verbosity and again is just a preview feature.

These are the 5 essential features of Java 14.


Posted

in

,

by

Tags:

Comments

2 responses to “What are the new features in Java 14 that a day to day Java developer should know?”

  1. Vinith Avatar
    Vinith

    The improvements made on handling null pointer exceptions will be very handy for programmers 😀

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading