Tuesday 11 March 2014

Unchecked Warning Messages

Generics are implemented in Java1.5. So Before 1.5 All the code written was not generic. So to support backward compatibility, Java allows Non generic code also.

Raw Type
Creating an object to a class of generic type, without specifying type parameters.

Class Box<T>{

}

Box is a Generic class.

Box b1 = new Box();
Above statement create a raw type for the class Box, since no type parameters are included.

The term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety. When you perform the operation on raw type, compiler throws unchecked warnings.

class Box<T>{
 String name;
 T data;

 Box(){

 }

 T getData(){
  return data;
 }

 public static void main(String args[]){
  Box<Integer> b1 = new Box<>();
  Box b2 = b1;
 }
}

Observe the program 'b2' is a raw type. Assigning b1 to b2 causes unchecked warnings.

To get the unchecked warnings compile the program wit -Xlint option.    








To suppress the unchecked warnings use -Xlint:unuchecked option while compiling.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment