This a follow-up to chrert\'s question Generic classes with Collection getter of other types. If you can come up with a better title to my question, feel free to edit it:
In the first case, raw.getCollection()
returns a raw Collection
. JLS 14.14.2 specifies the type checking for the enhanced for
loop:
If Type (in the FormalParameter production) is a reference type, then TargetType is Type; otherwise, TargetType is the upper bound of the capture conversion of the type argument of I, or Object if I is raw.
(emphasis added)
In the second case, you're explicitly assigning a raw type to a generic type, which is allowed with a warning like normal.