In this post, we will see about @SafeVarargs Annotation in Java 9.
Table of Contents
@SafeVarargs
is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, so the @SafeVarargs
annotation tells to the compiler to perform safe operations. For example, if we don’t use the annotation the compiler reports warning: Type safety: Potential heap pollution via varargs parameter
.
We can use this annotation to final
and static
and private
(from Java 9) methods only of a class. See the example below.
@SafeVarargs annotation
Let’s see few examples with final and static methods.
@SafeVarargs with Final Method
In this example, we have a final methods that takes var-args parameters so we used @SafeVarargs annotation. See the example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.List; class DemoPrint{ @SafeVarargs final void display(List<String>... lists) { for (List<String> list : lists) { System.out.println(list); } } } class Main { public static void main(String[] args){ DemoPrint dp = new DemoPrint(); List<String> list = new ArrayList<String>(); list.add("Python"); list.add("Java"); dp.display(list); } } |
Output
@SafeVarargs with Static Method
Java allows using @SafeVarargs
annotation with static methods as well. See the example with below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.ArrayList; import java.util.List; class DemoPrint{ @SafeVarargs static void display(List<String>... lists) { for (List<String> list : lists) { System.out.println(list); } } } class Main { public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("Python"); list.add("Java"); DemoPrint.display(list); } } |
Output
Java 9 @SafeVarargs Improvement
In Java 9, It is allowed to use @SafeVarargs annotation with private methods as well. Here, we have a private method displayString()
that takes string type var-args.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.ArrayList; import java.util.List; class DemoPrint{ @SafeVarargs static void display(List<String>... lists) { for (List<String> list : lists) { System.out.println(list); } } } class Main { @SafeVarargs private void displayString(String...strings ) { for (String str : strings) { System.out.println(str); } } public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("Python"); list.add("Java"); DemoPrint.display(list); Main main = new Main(); main.displayString("Java","is","a","Awesome","Language"); } } |
Output
is
a
Awesome
Language
Please note that if you use @SafeVarargs
in private method before Java 9, you will get a compilation error.
Without @SafeVarargs annotation
In case, we don’t use the annotation the compile will report a warning message to console with output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Main { private void displayString(String...strings ) { for (String str : strings) { System.out.println(str); } } public static void main(String[] args){ Main main = new Main(); main.displayString("Java","is","a","Awesome","Language"); } } |
Output
Note: Recompile with -Xlint:unchecked for details.
Java
is
a
Awesome
Language
That’s all about Java 9 @SafeVarargs Annotation.