×

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

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.