instanceof generics java code example
Example 1: instanceof java
class Example{
public static void main(String args[]){
Example anexample=new Example();
System.out.println(anexample instanceof Example);
}
}
Example 2: java return new instance of generic type
public static <E> void append(List<E> list, Class<E> cls) throws Exception {
E elem = cls.newInstance(); // OK
list.add(elem);
}
//You can invoke the append method as follows:
List<String> ls = new ArrayList<>();
append(ls, String.class);