I use @SuppressWarnings("unchecked")
and @SuppressWarnings("null")
mostly above methods to let the code compile without any warnings but I have my doubts. Found this Stackoverflow question. Jon Skeet wrote an answer to it which I find intriguing.
According to him,
Sometimes Java generics just doesn't let you do what you want to, and you need to effectively tell the compiler that what you're doing really will be legal at execution time.
But what if there is a chance that an exception will be thrown? Isn't suppressing warnings a bad idea then? Shouldn't I be aware of the places where problems could surface?
Also, what if someone else modifies my code later and adds some questionable functionality without removing SuppressWarnings? How can that be avoided and/or is there any other alternative to this?
Should I be using @SuppressWarnings("unchecked")
and @SuppressWarnings("null")
?
Update #1
As far as unchecked type casts go, according to this answer (pointed out by @gnat in the comments below), suppressing these warnings is necessary.
Many indispensible Java libraries have never been updated to eliminate the need for unsafe typecasts. Suppressing those warnings is necessary so that other more important warnings will be noticed and corrected.
In case of suppressing other warnings, still in a bit of a grey area.
Update #2
As per Oracle Docs (also mentioned by some answers below):
As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.