(nevermind:) it's illegal: HashSet<> f;
it's legal: new HashSet<>();
From type inference documentation:
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context.
But a compiler cannot infer type argument in second example, so why does it compile?
Object
. That rules of type inference are extremely long, but that's whatnew HashSet<>()
reduces to. – Sotirios Delimanolis