Ran into some questionable behavior using lists of parameterized Class objects:
ArrayList<Class<String>> classList = new ArrayList<Class<String>>();
classList.add(Integer.class); //compile error
Class intClass = Integer.class;
classList.add(intClass); //legal apparently, as long as intClass is not parameterized
Found the same behavior for LinkedList, haven’t tried other collections. Is it like this for a reason? Or have I stumbled on something?
That is legal only as a concession to backward compatibility so that code written before Java 5 can be compiled in 5 or later without going through and adding generics. Using a raw type for another reason is generally considered a programming error or at least poor style/laziness.
By willfully declaring a Raw Type you’re abandoning the compiler’s ability to help you with type safety, so you get a warning and it’s back on you to make sure things are safe at run time.