java define a generic class that produces
List<String> list1 = new ArrayList<String>(); // java 7 ? List<String> list1 = new ArrayList<>();
list1.add("abc");
//list1.add(new Integer(5)); //compiler error
for(String str : list1){
//no type casting needed, avoids ClassCastException
}
List list = new ArrayList();
list.add("abc");
list.add(new Integer(5)); //OK
for(Object obj : list){
//type casting leading to ClassCastException at runtime
String str=(String) obj;
}
Also in Java:
- map with insertion order preserved java
- java 8 stream option
- integer max value java
- how to open a folder in java swing project
- json request body not getting parsed by spring boot controller
- java map foreach
- how to make a dictionary in java
- how do I change the opacity of a JButton
- processing sound android
- foreach map java
- java arraylist
- java command line arguments
- how to pass parameters to xsl file
- java double format
- char array to string java
- java convert String to int
- java get command line output
- remove item from arraylist in java
- java variables in annotations
- java exponencial
- java string to boolean
- append to arraylist by index java
- how to extract data from delimited text file in java
- java - a program to print open ports