Skip to Content
Spring 5.0 Projects
book

Spring 5.0 Projects

by Nilang Patel
February 2019
Intermediate to advanced
442 pages
11h 46m
English
Packt Publishing
Content preview from Spring 5.0 Projects

Smart casts

While working with Java, you may face the scenario where you need to cast the object before further processing. However, even if there is certainty about the type of object that you are passing, you still need to cast the object explicitly in Java as follows:

//Java codepublic static void  main(String[] args){    Object name = "Nilang";    if(name instanceof String){        greetingMsg((String) name);    }}private static void greetingMsg(String name){    System.out.print(" Welcome "+name+" ..!!");}

If we try to call greetingMsg() without an explicit cast, the Java compiler will show an error because the name variable is of the Object type. In this case, although the compiler knows that name can be only of the String type (through the condition

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Spring Boot 2.0 Projects

Spring Boot 2.0 Projects

Mohamed Shazin Sadakath
Spring 5.0 By Example

Spring 5.0 By Example

Claudio Eduardo de Oliveira
Spring 5.0 Cookbook

Spring 5.0 Cookbook

Sherwin John C. Tragura
Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools

Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools

Iuliana Cosmina, Rob Harrop, Chris Schaefer, Clarence Ho

Publisher Resources

ISBN: 9781788390415Supplemental Content