February 2019
Intermediate to advanced
442 pages
11h 46m
English
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