The compiler may not be able to prove that your program is typesafe, but you can. You must convince yourself that the unchecked cast will not compromise the type safety of the program. The array in question (
elements) is stored in a private field and never returned to the client or passed to any other method. The only elements stored in the array are those passed to the
push method, which are of type
E, so the unchecked cast can do no harm.
Nice example how you can prove the warning generated for type safety by compiler can be proved to be not an issue
Share this highlighthttp://www.safaribooksonline.com/a/effective-java-2nd/4270872/