Campbell Ritchie wrote:You are telling the compiler and JVM that an object is a particular typeThe List interfce does not have an ensureCapacity method, but the java.util.ArrayList class does. So you can invoke the method on an ArrayList, not a LinkedList. So you tell the compiler that you have got an ArrayList here, and then you require space for 1000000 elements.
Note the instanceof operator ensures you have the correct type and won't suffer a ClassCastException; if you have a LinkedList, that invocation is skipped. One of the few places I like the use of instanceof.
Its not easy example to understand, it will not become clear to undestand.