import java.util.*;

// Short demonstration program to show type erasure
// Compile with javac TypeErasure.java
// Run with java TypeErasure
// Look at bytecodes with javap -c TypeErasure.class
//   (details don't matter except to observe that both
//    arraylists are treated identically in compiled
//    code and there is no type parameter information)

// CSE 331 19sp-22wi; Hal Perkins

public class TypeErasure {
  public static void main(String[] args) {
    List<String> lst1 = new ArrayList<String>();
    List<Integer> lst2 = new ArrayList<Integer>();

    // prints true!
    System.out.println(lst1.getClass() == lst2.getClass());
    
    // generic type rules enforced at compile time so there will
    // be no errors at runtime as long as no unchecked warnings,
    // even though generic type information is not present at runtime
    lst1.add("hello");
    // lst1.add(new Integer(17));  // won't compile
    String s = lst1.get(0);        // can't fail unless type system bypassed

    // cannot use instanceof to discover generic type parameters
    System.out.println(lst1 instanceof Collection);     // true
    System.out.println(lst1 instanceof Collection<?>);  // true
    // System.out.println(lst1 instanceof Collection<String>);  // won't compile
  }
}