1
2List list = new ArrayList();
3list.add("abc");
4list.add(new Integer(5)); //OK
5
6for(Object obj : list){
7 //type casting leading to ClassCastException at runtime
8 String str=(String) obj;
9}
10
1
2List<String> list1 = new ArrayList<String>(); // java 7 ? List<String> list1 = new ArrayList<>();
3list1.add("abc");
4//list1.add(new Integer(5)); //compiler error
5
6for(String str : list1){
7 //no type casting needed, avoids ClassCastException
8}
9