×

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14.

Features in Java 14

  1. Switch Expression
  2. Text Blocks
  3. Records
  4. Null Pointer Exceptions
  5. Instance of
  6. Packaging Tool
  7. Garbage collectors

1.Switch Expressions

Switch expressions were first released in Java 12 versions. But it was only in preview until Java 14 was released in which it was standardized.

Main.java

import java.util.*;
public class Main{  
public static void main(String[] args) {  
    //Declaring a variable for switch expression  
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the number");
    int n=sc.nextInt();
    //Switch  expression is declared  
    switch(n){  
    //Different Case statements  are declared 
    case 10: System.out.println("Entered number is 10");  
    break;  
    case 20: System.out.println("Entered number is  20");  
    break;  
    case 30: System.out.println("Entered number is 30");  
    break;  
    //Default case statement  
    default:System.out.println("Number is not in 10, 20 or 30");  
    }  
}  
} 

By using the switch expression, we can modify the code into easier way

Main.java

public class Main{  
public static void main(String[] args) {  
    //Declaring a variable for switch expression  
    String n= "Cse";
    String result= " ";
    //Switch  expression is declared  
    switch(n)
    {  
    //Different Case statements  are declared 
    case "Cse" -> result=" computers"; 
    case "ece" -> result= " electrical";  
 
    //Default case statement  
    default-> result="B.Tech";
    }  
    System.out.println(result);
}  
}

2. Text Blocks

Additional two escape characters were added to Java 14 version compared to Java 13 version.

  • \: Indicates the end of the line
  • \s: Indicates a single space

Previously in Java 13 the text blocks were written as below

Main.java

import java.io.*;
import java.util.*;
public class Main{  
public static void main(String[] args) 
{  
    // text is given
    String text = "Hello everyone ; Welcome to Java 14 Features";
    System.out.println(text);
}  // Main
} // class

Now with the new features of Java 14 we can write the above text as

Main.java

import java.io.*;
import java.util.*;
public class Main{  
public static void main(String[] args) 
{  
    // text is given
   String text =" Hi  Everyone \n Welocme";
    System.out.println(text);
}  // Main
} // class

Output:

New Features of Java 14

3. Pattern Matching

We use the instance of keyword to check whether the object is an instance of a class or not.

Main.java

import java.io.*;
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Main Obj = new Main();
    System.out.println(Obj instanceof Main); 
// prints true
  }
}

4. Records

  • Within a record, you can utilize nested classes and interfaces.
  • You can also have nested records, which are automatically static.
  • Interfaces may be implemented by a record.
  • A generic record class can be made.
  • Serializing data is possible.

5. Null Pointer Exceptions

When an object reference to a null value,  then the NullPointerException is raised.

TestSingleton.java

import java.util.UUID;
import java.io.*;
class Main
{
    private static Singleton single = null;
    private String ID = null;
     Singleton()
    {
        ID = UUID.randomUUID().toString();
    }
    public static Singleton getInstance()
    {
        if (single == null)
            single = new Singleton();
        return single;
    }
    public String getID()
    {
        return this.ID;
    }
}
// Driver Code
public class TestSingleton
{
    public static void main(String[] args)
    {
        Singleton s = Singleton.getInstance();
        System.out.println(s.getID());
    }
}

6. Memory Accesses

The Java 14 provides Java API, allowing Java applications to access external memory that is stored safely and effectively outside the Java heap.


Related Topics

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.