This content has been marked as final. Show 2 replies
rbbrooks wrote:And that's exactly the point. Generics guarantee that you won't get ClassCastExceptions from using them if and only if you don't use raw types anywhere and have no compiler warnings.
This code throws an unchecked warning, but still allows the assignment.
Raw types have no point in new code (which is why you get a compiler warning) and are an easy way to break the guarantees of generics.
It seems that the compiler should throw an error instead.By the way it doesn't "throw a warning" or "throw an error". The compiler produces a warning. "throw" is only used when talking about Exceptions (actually Throwable) that happen at runtime.
I suppose this conversion might be allowed in order to work with legacy code without the use of generics, but it seems like a great vulnerability for developers.It's only a problem if you ignore the warning.