Skip to content

Instantly share code, notes, and snippets.

@95Rajitha
Last active May 11, 2021 09:51
Show Gist options
  • Save 95Rajitha/4ab7f411bbdf1e50d65d661d875de4d9 to your computer and use it in GitHub Desktop.
Save 95Rajitha/4ab7f411bbdf1e50d65d661d875de4d9 to your computer and use it in GitHub Desktop.
Method level type erasure for Unbound
// Following method decleration with generic types
public static <E> void countElements(E[] array) {
private int count=0;
for (E element : array) {
count++;
}
System.out.println(count);
}
// compiler replaces the type parameter E with Object
public static <E> void countElements(Object[] array) {
private int count=0;
for (Object element : array) {
count++;
}
System.out.println(count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment