// Using generics to convert run-time exceptions into compile-time exceptions
import java.util.*;
class WithGenerics {
public static void main( String[ ] args ) {
// Creating an ArrayList with String specified
ArrayList<String> al = new ArrayList<String>( );
al.add( args[0] );
al.add( args[1] );
// Now compiler doesn't allow the following command.
al.add( Integer.parseInt( args[2] ) );
String s1 = (String) al.get( 0 );
String s2 = (String) al.get( 1 );
String s3 = (String) al.get( 2 );
}
}
|