Java Generics with primitive types

Question

What is the output of the following code?

class Gen<T> { 
  private T ob; // declare an object of type T 

  Gen(T o) { /*from ww w  . ja v  a  2  s  . c o m*/
    ob = o; 
  } 
 
  // Return ob. 
  public T getob() { 
    return ob; 
  } 
   // Show type of T. 
  void showType() { 
    System.out.println("Type of T is " + 
                       ob.getClass().getName()); 
  } 
} 

public class Main { 
  public static void main(String args[]) { 
    Gen<int> iOb = new Gen<int>(88); 
    iOb.showType(); 
    int v = iOb.getob(); 
    System.out.println("value: " + v); 
 
    Gen<String> strOb = new Gen<String>("Generics Test"); 
    strOb.showType(); 
    String str = strOb.getob(); 
    System.out.println("value: " + str); 
  } 
}


Compile time error

Note

The type argument passed to the generic type parameter must be a reference type.

You cannot use a primitive type, such as int or char.




PreviousNext

Related