New page: Properly used Covariance is said to be source compatible, but there is one language contruct which can lead to broken code - '''instanceof'''. The JavaC act...

New page

Properly used [[Covariance]] is said to be [[SourceCompatibility|source compatible]], but there is one language contruct which can lead to broken code - '''instanceof'''. The [[JavaC]] acts cleverly and checks not just lower bound of the first parameter, but upper bound. For example following code is not going to compile:

<source lang="java">
String s = "";
assert s instanceof Integer;
</source>

The [[JavaC]] complains about ''inconvertible types''. Simple change to the program fixes the problem:

<source lang="java">
Object s = "";
assert s instanceof Integer;
</source>

Knowing this [[JavaC]] behavior, I am sure you can write code that will compile with the [[API]] used in [[Covariance]] example version 1.0 and will break the compilation against version 2.0. Am I right?