I didn't understand this feature of Java. I know it makes coding easier and sometimes looks neater, but what is the actual use of this? On contrary i feel, its better to display the warnings, as in future any one can refer them before making modifications to code. Does this @SuppressWarnings increases compliling efficiency OR is this according to any coding standards?
asked Dec 20, 2011 at 9:22 freepublicview freepublicview 726 2 2 gold badges 13 13 silver badges 25 25 bronze badgesWhile programming you should watch out for compiler warnings and in best case compile your code with no warnings (and errors of course).
But sometimes you can't get rid of a warning and you KNOW the code is correct or can't be changed. Then you don't want to be bothered every time from the compiler that there is something wrong.
So you can suppress it with the command you mentioned.
answered Dec 20, 2011 at 9:30 204k 38 38 gold badges 300 300 silver badges 372 372 bronze badgesOther answers already explained use cases of @SuppressWarnings a lot, but I want to emphasize the point that sometimes you absolutely need to use @SuppressWarnings to overcome limitations of the language itself, and in these cases use of @SuppressWarnings is absolutely legal.
In other cases use of @SuppressWarnings can be considered questionable, because in these cases you can always get rid of warnings by changing the code (though, obviously, it's not always acceptable).
Here are some common cases when you absolutely cannot get rid of warnings without @SuppressWarnings :
When you are dealing with legacy code that does not support generics (Java answered Dec 20, 2011 at 9:25 Wojciech Owczarczyk Wojciech Owczarczyk 5,715 2 2 gold badges 34 34 silver badges 56 56 bronze badges SuppressWarnings are not only used for Generics support or cast warnings. Commented Dec 20, 2011 at 9:31
I only said it is the only way of removing cast warnings due to lack of generics in older parts of the code, when at some point you would like to introduce them. I haven't said it is the only use of this annotation.