Jay Chung wrote:in this case, I would've thought that line * follows rule B, but if that were the case, then it would be
Predicate p = Predicate(), which then should cause * to NOT COMPILE as it does not implement
public boolean test(Object o)
Your interpretation is pretty good, but a little off. In rule B, it's not that the compiler treats new ObjectInstance<Type>() as new ObjectInstance() for the entire statement. Rather, it makes that substitution, ignoring the generic parameter,
while evaluating the assignment part (the =). So think of the compiler as looking at the expression in different parts. First:
This part makes sense on its own - a CourseFilter needs to have a boolean test(
String). Great! The compiler is happy with this.
And then this part:
When the compiler looks at the assignment, it basically asks itself, can the thing on the right be treated as the thing on the left? Which in this case means, can a Predicate<String> be treated as a Predicate? The answer is yes, it's willing to ignore the generic part and let the assignment take place. If you want to treat this as a raw Predicate, you can, the compiler will let you. However that doesn't mean it's re-evaluating the entire right hand side to make it conform to the raw Predicate type. The right hand is still a Predicate<String>, with a test(String) method. And the assignment is allowed to happen.
Now at run time, you may still run into problems. What happens if you run the following?