In this quick article, we will discuss how to use @SafeVarargs annotation with examples.
Java 7 introduced the @SafeVarargs annotation to suppress the unsafe operation warnings that arises when a method is having varargs (variable number of arguments).
From Oracle Doc: @SafeVarargs annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its varargs parameter. When this annotation type is used, unchecked warnings relating to varargs usage are suppressed.
Below diagram shows the internal implementation of @SafeVarargs annotation for your reference:
@SafeVarargs Annotation Example
package net.javaguides.annotations;
import java.util.ArrayList;
import java.util.List;
public class SafeVarAnnotationExample {
@SafeVarargs
private void print(List...names) {
for (List < String > name: names) {
System.out.println(name);
}
}
public static void main(String[] args) {
SafeVarAnnotationExample obj = new SafeVarAnnotationExample();
List < String > list = new ArrayList < String > ();
list.add("Ramesh");
list.add("John");
obj.print(list);
}
}
Output:
[Ramesh, John]
Note that Java 9 extended the use of @SafeVarargs annotation, it can now be used with private methods as well. This is because private methods cannot be overridden. Earlier this annotation was limited to the final or static methods, or constructors but now it can be used with private methods.
Java 9 Example – When we do not use @SafeVarargs annotation?
package net.javaguides.annotations;
import java.util.ArrayList;
import java.util.List;
public class SafeVarAnnotationExample {
private void print(List...names) {
for (List < String > name: names) {
System.out.println(name);
}
}
public static void main(String[] args) {
SafeVarAnnotationExample obj = new SafeVarAnnotationExample();
List < String > list = new ArrayList < String > ();
list.add("Ramesh");
list.add("John");
obj.print(list);
}
}
Warnings: Compiler shows below are warning messages.
Type safety: Potential heap pollution via varargs parameter names
Type safety: A generic array of List is created for a varargs parameter
Output:
[Ramesh, John]
Java 9 – @SafeVarargs Annotation Example
Let's run the same code again after using the @SafeVarargs annotation.
package net.javaguides.annotations;
import java.util.ArrayList;
import java.util.List;
public class SafeVarAnnotationExample {
@SafeVarargs
private void print(List...names) {
for (List < String > name: names) {
System.out.println(name);
}
}
public static void main(String[] args) {
SafeVarAnnotationExample obj = new SafeVarAnnotationExample();
List < String > list = new ArrayList < String > ();
list.add("Ramesh");
list.add("John");
obj.print(list);
}
}
Output:
[Ramesh, John]
The same output without any warnings.
Reference
- https://docs.oracle.com/javase/8/docs/api/java/lang/SafeVarargs.html
- https://www.javatpoint.com/java-9-safevarargs-annotation
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment