Home arrow static arrow Java Programming [Archive] - Compiler Question related to the instanceof
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Compiler Question related to the instanceof
This topic has 2 replies on 1 page.

Posts:22
Registered: 6/21/04
Compiler Question related to the instanceof  
Jun 25, 2004 9:21 PM



 
Hi Everyone,

I have come across a paragraph in a book that is a little confusing, I am hoping that somebody out there will be able to help clarify what is meant:

"Some compilers, such as Sun's JDK, will not allow code to compile if they can determine with the available classes that a particular object cannot possibly inherit from a class specified in instanceof."

Does this mean that if you test for an instanceof which is false the class will not compile? I don't get this. Any explenations will be greatly appreciated.

Thanks

Regards

Davo
 

Posts:2,830
Registered: 9/1/03
Re: Compiler Question related to the instanceof  
Jun 25, 2004 9:30 PM (reply 1 of 2)



 
consider this example:
class InstOf {	static {		Object obj = null;		if(obj instanceof Integer){			System.out.println("possible, but not true");		}		Integer in = null;		if(in instanceof String){			System.out.println("never possible.");		}	}}
run it and see the effect. code fails. second instanceof cannot possibly
be true, first one may be true.
 

Posts:147
Registered: 26.01.04
Re: Compiler Question related to the instanceof  
Jun 26, 2004 9:08 AM (reply 2 of 2)



 
the instanceof test requires a lot of runtime, thats why the compiler already tells u that test will fail.

sometimes its even better to put the cast in a try-catch block and handle exceptions.
 
This topic has 2 replies on 1 page.