Sunday, 29 June 2014

@SafeVarargs

A variable arity parameter(Varargs) with a non-reifiable element type can cause heap pollution and give rise to compile-time unchecked warnings. Such warnings are uninformative if the body of the variable arity method is well behaved with respect to the variable arity parameter.

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);
 }
}

Output
Hi

1. It is a compile-time error if a fixed arity method or constructor declaration is annotated with the annotation @SafeVarargs.

import java.util.*;
import java.lang.*;

class SafeVarargsEx1{
 @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<> ();
 
  myList1.add("Hi");
  
  call(myList1);
 }
}

When you tries to compile the above program, compiler throws below error.

SafeVarargsEx1.java:6: error: Invalid SafeVarargs annotation. Method call(List<S
tring>) is not a varargs method.
        static void call(List<String> stringLists) {
                    ^
SafeVarargsEx1.java:7: error: array required, but List<String> found
                String s = stringLists[0].get(0);
                                      ^
2 errors

2. @SafeVarargs is only applicable to static methods, final instance methods, and constructors, the annotation is not usable where method overriding occurs.

import java.util.*;
import java.lang.*;

class SafeVarargsEx2{
 @SafeVarargs
 void call(List<String>... stringLists) {
  String s = stringLists[0].get(0);
  System.out.println(s);
 }
}

When you tries to compile the above program, compiler throws below error.

SafeVarargsEx2.java:6: error: Invalid SafeVarargs annotation. Instance method call(List<String>...) is not final.
        void call(List<String>... stringLists) {
             ^
1 error





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment