By
using @SafeVarargs annotation, we can tell the compiler that the usage of
varargs and generics is safe . @SafeVarargs can only be applied to methods which
cannot be overridden. Prior to Java9, @SafeVarargs is applied to only
c.
Constructors
Another
kind of non-overridable executable is a private method. From Java9 onwards, you
can apply @SafeVarargs annotation on top of private methods also.
What are varargs?
In
java we can create methods that takes variable number of arguments. This
feature is called varargs or variable number of arguments. The method which
accepts variable number of arguments is called varargs method.
Syntax
returnType methodName(dataType …
parameterName){
}
"..."
tells the compiler that this is a vararg.
class VarargsEx{ public void printData(int ... a){ System.out.println("Parameters are " + a.length); for(int i=0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } public static void main(String args[]){ VarargsEx obj = new VarargsEx(); obj.printData(1); obj.printData(1,2); obj.printData(1,2,3); } }
Output
Parameters are 1 1 Parameters are 2 1 2 Parameters are 3 1 2 3
The
syntax of the method printData, tells the compiler, that this method can
accept zero or more arguments. So all the calls like below are valid.
obj.printData(1);
obj.printData(1,2);
obj.printData(1,2,3);
A
varargs parameter of type T... is converted into a parameter of type T[]. By
applying @SafeVarargs on a method or constructor makes sure the compiler these
methods don’t perform potentially unsafe operations on its varargs parameter. Applying
this annotation to a method or constructor suppresses unchecked warnings about
a non-reifiable variable arity (vararg) type and suppresses unchecked warnings
about parameterized array creation at call sites.
import java.util.*; import java.lang.*; class SafeVarargsEx{ static void call(List<String>... stringLists) { String s = stringLists[0].get(0); System.out.println(s); } public static void main(String args[]){ List<String> myList1 = new ArrayList<> (); List<String> myList2 = new ArrayList<> (); myList1.add("Hi"); myList2.add("Hi"); call(myList1, myList2); } }
When
you tries to compile the above program, compiler throws below warning messages.
Note: SafeVarargsEx.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
To
suppress the above warnings use @SafeVarargs Annotation.
import java.util.*; import java.lang.*; class SafeVarargsEx{ @SafeVarargs static void call(List<String>... stringLists) { String s = stringLists[0].get(0); System.out.println(s); } public static void main(String args[]){ List<String> myList1 = new ArrayList<> (); List<String> myList2 = new ArrayList<> (); myList1.add("Hi"); myList2.add("Hi"); call(myList1, myList2); } }
@SafeVarargs
can only be applied to methods which cannot be overridden. Prior to Java9,
@SafeVarargs is applied to only
a.
Static
methods
b.
Final
methods
c.
Constructors
Another
kind of non-overridable executable is a private method. Therefore, it would be
reasonable to allow @SafeVarargs to be applied to private methods too. From
Java9 onwards, it is possible to apply @SafeVarargs annotation on private
methods.
Reference
No comments:
Post a Comment