What are raw types in generics in Java?


Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.

To define a generic class you need to specify the type parameter you are using in the angle brackets “<>” after the class name and you can treat this as datatype of the instance variable an proceed with the code.

Example − Generic class

class Person<T>{
   T age;
   Person(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value of age: "+this.age);
   }
}

Usage − While instantiating the generic class you need to specify the object name after the class within the angular brackets. Thus, choose the type of the type parameter dynamically and pass the required object as a parameter.

public class GenericClassExample {
   public static void main(String args[]) {
   Person<Float> std1 = new Person<Float>(25.5f);
      std1.display();
      Person<String> std2 = new Person<String>("25");
      std2.display();
      Person<Integer> std3 = new Person<Integer>(25);
      std3.display();
   }
}

Raw types

While creating an object of the generic class or interface if you do not mention the type arguments they are known as raw types.

For example, if you observe the above example while instantiating the Person class you need to specify type for the type parameter (type of a class) in angle brackets.

If you avoid specifying any type parameters within angular brackets and create an object of a generic class or, interface they bare known as raw types.

public class GenericClassExample {
   public static void main(String args[]) {
      Person per = new Person("1254");
      std1.display();
   }
}

Warning

These will compile without error but generates a warning as shown below.

Note: GenericClassExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details

Following are some notable points about raw types −

  • You can assign a parameterized (generic) type to its raw type.
public class GenericClassExample {
   public static void main(String args[]) {
     Person per = new Person(new Object());
       per = new Person<String>("25");
      per.display();
    }
}

Output

Value of age: 25
  • A warning will be generated if you assign a raw type to a parameterized type.
public class GenericClassExample {
   public static void main(String args[]) {
      Person<String> obj = new Person<String>("");
      obj = new Person(new Object());
      obj.display();
   }
}

Warning

Note: GenericClassExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
  • If your generic class contains a generic method and if you try to invoke it using a raw type a warning will be generated at the compile time.

public class GenericClassExample {
   public static void main(String args[]) {
      Student std = new Student("Raju");
      System.out.println(std.getValue());
   }
}

Warning

Note: GenericClassExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
raja
Published on 09-Sep-2019 11:45:30
Advertisements