instanceof vs class.isInstance

instanceof is resolved and compile time where as class.isInstance() is dynamic in the sense that

String x =””;

x instanceof String => true. The only limitation is that instanceof takes 2 operators one a variable and the other variable is class name which is constant and determined at compile time.

In class.isInstance() class doesn’t need to be determined at compile time, it can be a variable and resolved at runTime.

null is not an instanceof any java type.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s