Why we use suppresswarnings




















Warnings are useful items of information. Make sure you heed the ones that speak the truth, and filter away the ones that don't. Don't let people commingle the two kinds so that you lose your early warning system. I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it.

However, there are areas in which the compiler cannot be sure whether something will be a problem or not Java's generics are one such area , and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.

A warning means: The compiler found something that looks dodgy. It doesn't mean it is dodgy, it just looks like it to the compiler. Sometimes you have code that is perfectly fine and gives a warning. Sometimes you fix it by slightly modifying your code. Sometimes the compiler has some feature specifically for that purpose.

For example where. In the second case no warning because the extra parentheses tell the compiler "I know what I'm doing". Better of course would be. And sometimes, very rarely, there are cases when your code is fine but you can't manage to write it in a way that is accepted without warning.

In that very, very rare case you disable a warning for that statement and turn it on immediately afterwards. That's a last resort. And you only disable that specific warning, no others. However, some people just disable warnings to get rid of warnings, because they are too lazy to find out and fix the reason for a legitimate warning first.

Or they don't even try to write code that is free of warnings. That's an extremely unhealthy thing to do. Suppressing warning for an entire method is suspect. Better to suppress the warnings for the specific line, with a comment. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. Is it a good practice to use suppress warnings in your code? Viewed k times. Sometime when looking through code, I see many methods specify an annotation: SuppressWarnings "unchecked" What does this mean?

Improve this question. Tom Hawtin - tackline k 30 30 gold badges silver badges bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Jon Skeet Jon Skeet 1. In some cases, you can avoid it by using YourClazz. Works for single generic element containers but not for collections. This won't always work however see generics FAQ for details. You can read more on this specific annotation here: SuppressWarnings Additionally, Oracle provides some tutorial documentation on the usage of annotations here: Annotations As they put it, "The 'unchecked' warning can occur when interfacing with legacy code written before the advent of generics discussed in the lesson titled Generics.

Keshaw Kumar 1 1 gold badge 5 5 silver badges 14 14 bronze badges. Your SupressWarning link is dead; here's an alternative: docs. Simply: It's a warning by which the compiler indicates that it cannot ensure type safety. This is a good example for the situation where we need to use SuppressWarnings. In Java, generics are implemented by means of type erasure. For instance, the following code.

But now add this line. ClassCastException: java. String cannot be cast to java. Integer An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Konrad Borowski Konrad Borowski Bill the Lizard k gold badges silver badges bronze badges. One trick is to create an interface that extends a generic base interface Bilesh Ganguly 3, 3 3 gold badges 34 34 silver badges 53 53 bronze badges.

Brian Edwards Brian Edwards 3 3 silver badges 5 5 bronze badges. JDK5 is new? It has completed most of its End of Service Life period. I know that JDK 5 is rather outdated, what I meant was that, it is new in the sense that it introduced new features to Java, previously not offered in JDK 4. TreeSet set. Object required: T clon. Konrad Borowski Majid Roustaei Majid Roustaei 1 1 gold badge 11 11 silver badges 29 29 bronze badges.

The Overflow Blog. Podcast Explaining the semiconductor shortage, and how it might end. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Last Updated : 24 Oct, SuppressWarnings "unchecked". SuppressWarnings "deprecation".



0コメント

  • 1000 / 1000