Most references I’ve seen, and my IDE’s code completion all have my specifying a Generic type on both a variables type and its implementation eg.
List<String> string = new ArrayList<String>();
Recently I’ve started skipping the generic type on the implementation eg.
List<String> strings = new ArrayList();
assuming that the compiler is only checking against the type for it’s spiffy generics voodoo, and that the implementation shouldn’t care, as its generic types are erased when it’s compiled and should not affect run-time at all.
Am I being naive? Is there a reason to include the generic type on the implementation?
It’s required as it won’t necessarily be the same:
One way of avoiding it for common cases is to have static utility methods, and let the compiler’s type inference do the rest:
This is type safe, and doesn’t require the generic type on the implementation. (The reason itay’s example is not safe the lack of the generic type on the type of the list variable, not anything to do whether you need to specify generic type on both the variable’s type and the implementation.)