Skip to main content

Java For Python Programmers Edition 2

Section 9.6 Forgetting to declare the kind of object in a container

Java is a statically typed language, meaning you must specify the type of objects that can be stored in a container like an ArrayList. If you forget to declare the type, the compiler will give you an error.
This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the add() method on rawList, which is an ArrayList used as a raw type (i.e., without specifying a generic type like <String> or <Integer>).
When ArrayList is used as a raw type, the compiler cannot guarantee the type of elements being added or retrieved, leading to "unchecked" operations. The E extends Object part refers to the generic type parameter E that ArrayList should have, which defaults to Object when the type is not specified, making the add() call unchecked. This can lead to ClassCastExceptions at runtime if incompatible types are later retrieved and cast.
You have attempted of activities on this page.