Sometime when looking through code, I see many methods specify an annotation:
@SuppressWarnings("unchecked")
What does this mean?
Sometime when looking through code, I see many methods specify an annotation:
@SuppressWarnings("unchecked")
What does this mean?
As far I know, for now it has to do with suppressing warnings about generics; generics are a new programming construct not supported in JDK versions earlier than JDK 5, so any mixes of the old constructs with the new ones might pose some unexpected results.
The compiler warns the programmer about it, but if the programmer already knows, they can turn those dreaded warnings off using SuppressWarnings.
It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions), such as casts. It essentially implies that the programmer did not wish to be notified about these which he is already aware of when compiling a particular bit of code.
You can read more on this specific annotation here:
Additionally, Oracle provides some tutorial documentation on the usage of annotations here:
As they put it,
Simply: It's a warning by which the compiler indicates that it cannot ensure type safety.
JPA service method for example:
If I didn'n anotate the @SuppressWarnings("unchecked") here, it would have a problem with line, where I want to return my ResultList.
In shortcut type-safety means: A program is considered type-safe if it compiles without errors and warnings and does not raise any unexpected ClassCastException s at runtime.
I build on http://www.angelikalanger.com/GenericsFAQ/FAQSections/Fundamentals.html
The SuppressWarning annotation is used to suppress compiler warnings for the annotated element. Specifically, the
unchecked
category allows suppression of compiler warnings generated as a result of unchecked type casts.