Java complains so much when you apply a non-parameterized type (object) to a parameterized type (LinkedList). This is to tell you that it can be anything. This really is no different from the first cast, except for the first one that throws a ClassCastException, if it's not that type, but the second won't.
It all comes down to erasing the type. LinkedList at runtime is just a LinkedList. You can put anything into it and it will not throw a ClassCastException, as in the first example.
Often, to get rid of this warning, you have to do something like:
@SuppressWarning("unchecked") public List<Something> getAll() { return getSqlMapClient.queryForList("queryname"); }
where queryForList () returns a list (non-parameterized), where you know that the content will have the class Something.
Another aspect of this is that arrays in Java are covariant , which means that they store information like runtime. For instance:
Integer ints[] = new Integer[10]; Object objs[] = ints; objs[3] = "hello";
will throw an exception. But:
List<Integer> ints = new ArrayList<Integer>(10); List<Object> objs = (List<Object>)ints; objs.add("hello");
is completely legal.
source share