I am reading about Generics in Effective Java item 23. It is mentioned as below:
While the prospect of accidentally inserting a coin into a stamp collection may appear far-fetched, the problem is real. For example, it is easy to imagine someone putting a
java.util.Date
instance into a collection that is supposed to contain onlyjava.sql.Date
instances.
I am not able to understand what does author mean by "it is easy to imagine someone putting a java.util.Date
instance into a collection that is supposed to contain only java.sql.Date
instances"?
Collection<java.util.Date> dateColl = new ArrayList();
String strDate = "2011-12-31 00:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
java.util.Date date = sdf.parse(strDate);
java.sql.Date sqlDate = new Date(date.getTime());
dateColl.add(sqlDate);
Why above code is not throwing compilation error?