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 😃
Nice explanation. Thank you.
ReplyDeleteGood explanation
ReplyDeleteHi 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!
ReplyDeleteNice post.Thanks for posting.
ReplyDeleteFull stack classes in Pune