Abstract


Covariant

Covariance in Java arrays can lead to runtime errors

Java Arrays are Covariant.

  • Imagine you have an array of Integer.
  • Due to covariance, you can assign this array to a variable declared as an array of Object.
  • Now, you might try to add a String to this array (which seems okay since it’s an array of Object).
  • However, at runtime, Java discovers you’re trying to put a String into an array meant for Integer, causing an error.

Contravariant

Invariant

Invariants in Java

Primitive arrays and Generics are invariant.