Instanceof Enhancement

Before Java 16, To type cast from super class to sub class type, We first need to check with  'instanceof ' operator to know underlying object type to avoid exception.

Object stringObj = "Fullstack Adda";

if (stringObj instanceof String) {

String string = (String) stringObj;

System.out.println(string);

}

Output:

Fullstack Adda


The above code can be written as follows.

Object stringObj = "Fullstack Adda";

if (stringObj instanceof String string) {

System.out.println(string);

}

Output:

Fullstack Adda

This enhancement helps to reduce the code statements.

 Happy Coding 😃 

4 comments:

  1. Hi Team, this blog is extraordinary and the information tells about Full Stack Adda in Java16. You have explained the details about its type and sub cast coding which is clearly explained in the way of understanding. Kudos for your info!!! For getting the best Full Stack Developer Course in Chennai, reach to Infycle Technologies!

    ReplyDelete

Different ways to run Spring boot App

 What are the different ways to run Spring boot app ?