Source of ProblemSolvedByGenerics.java


  1: //ProblemSolvedByGenerics.java

  3: import java.util.ArrayList;
  4: import java.util.List;
  5: import java.util.Date;

  7: public class ProblemSolvedByGenerics
  8: {
  9:     public static void main(String[] args)
 10:     {
 11:         List<Date> myList = new ArrayList<>();
 12:         myList.add(new Date()); //OK
 13:         //myList.add(new String("Hello, world!")); //Won't compile!
 14:         //myList.add(Integer.valueOf(6)); //Won't compile!
 15:         System.out.println(myList.get(0)); //Note: No cast necessary!
 16:     }
 17: }
 18: /*  Output:
 19:     Thu Jul 26 16:35:00 ADT 2018
 20: */