The @SuppressWarnings Annotation in Java
Last Updated :
24 Oct, 2021
Annotations are a very important part of Java in modern technologies, Most of the technologies such as Hibernate, Spring, Spring Boot, JPA, and so Many other Libraries are using annotations and making developers’ life a lot easy. In Java, built-in General Annotations are –
- @Override
- @Deprecated
- @FunctionalInterface
- @SuppressWarnings
Syntax: The signature for Java @SuppressWarnings annotation is as follows:
@Retention(value=SOURCE)
@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })
public @interface SuppressWarnings {
String[] value;
}
As we can see, the above signature has only one element, which is Array of String, with multiple possible values.
All annotations have two properties :
- Target (@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })) – It will be used with almost everything, wherever you want to suppress warnings.
- Retention (@Retention(value=SOURCE)): Retention policy of functional Interface “SOURCE”, which means annotation won’t go till compiler.
Illustrations:
Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code.
1. @SuppressWarnings("unchecked")
public class Calculator {
}
- Here, it will ignore all unchecked warnings coming from that class. (All methods, variables, constructors).
2. public class Calculator {
@SuppressWarnings("unchecked")
public int sum(x,y) {
.
}
}
- It will stop warning from that function only, and not from other functions of Calculator class.
This annotation is dangerous because a warning is something potentially wrong with the code. So if we’re getting any warning, the first approach should be resolving those errors. But if we’re suppressing any warnings, we have to have some solid reason. The reason should be commented near to the annotation every time it is used.
Possible Values Inside @SuppressWarnings Annotation Element are as follows:
Values
|
Description |
All |
It will suppress all warnings. |
Cast |
Suppress the warning while casting from a generic type to a nonqualified type or the other way around. |
Deprecation |
Ignores when we’re using a deprecated(no longer important) method or type. |
divzero |
Suppresses division by zero warning. |
empty |
Ignores warning of a statement with an empty body. |
unchecked |
It doesn’t check if the data type is Object or primitive. |
fallthrough |
Ignores fall-through on switch statements usually (if “break” is missing). |
hiding |
It suppresses warnings relative to locals that hide variable |
serial |
It makes the compiler shut up about a missing serialVersionUID. |
finally |
Avoids warnings relative to finally block that doesn’t return. |
unused |
To suppress warnings relative to unused code. |
Note: The primary and most important benefit of using @SuppressWarnings Annotation is that if we stuck because of some known warning, then this will ignore the warning and move ahead. E.g. – deprecated and unchecked warnings.
Example:
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Addition {
public static int sum( int n1, int n2)
{
return n1 + n2;
}
public static int sum( int ... nums)
{
int sum = 0 ;
for ( int i : nums) {
sum += i;
}
return sum;
}
}
public class GFG {
@SuppressWarnings ( "unchecked" )
public static void main(String[] args)
{
Addition add = new Addition();
@SuppressWarnings ( "deprecation" )
int sum = Addition.sum( 10 , 20 );
System.out.println( "Sum of 10 and 20 : " + sum);
@SuppressWarnings ( "rawtypes" )
List list = new ArrayList();
list.add( 12 );
list.add( 120 );
System.out.println( "List items : " + list);
}
}
|
Output
Sum of 10 and 20 : 30
List items : [12, 120]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...