Skip to content

Instantly share code, notes, and snippets.

@nicholashagen
Created February 7, 2012 16:23
Show Gist options
  • Save nicholashagen/1760526 to your computer and use it in GitHub Desktop.
Save nicholashagen/1760526 to your computer and use it in GitHub Desktop.
Java Type Erasure Demonstration
// TEST 1: what happens in this scenario?
List<String> test1 = new ArrayList<String>();
test1.add(new Integer(5));
// TEST 2: what happens and/or is printed in this scenario?
List<String> test2 = new ArrayList<String>();
Method method2 =
test2.getClass().getMethod("add", String.class);
System.out.println(method2);
// TEST 3: what happens in this scenario?
List<String> test3 = new ArrayList<String>();
Method method3 =
test3.getClass().getMethod("add", Object.class);
method3.invoke(test3, new Integer(5));
// TEST 4: what happens or is printed in this scenario
String result3 = test3.get(0);
System.out.println("RESULT 3: " + result3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment