I have a problem with the following code:
public static <T> T firstNonNull(@Nullable T first, @Nullable T second) { return first != null ? first : second; } public static Set<String> getStrings() { return new HashSet<>(); } public static Set<String> doesNotCompile = firstNonNull(getStrings(), new HashSet<>());
With JDK 8 until update 11, this code compile. With JDK 8 update 20, it does not compile anymore. In the last statement, I have to explicitly specify the String
type argument for the last HashSet
instantiation.
I was wondering if I am wrong with this code or if it is a regression in the last JDK update.
Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.
The full version string for this update release is 8u331-b09 (where "b" means "build"). The version number is 8u331.
http://www.oracle.com/technetwork/java/javase/jdk8-naming-2157130.html Java SE Development Kit 8, also known as JDK 8, has the version number 1.8. In short – 8 is product version number and 1.8 is the developer version number (or internal version number). The product is the same, JDK 8, anyways.
Type inference represents the Java compiler's ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned.
This is a new bug that exists in the JDK 8u20 update release and in the current JDK 9 development branch. This code worked before, so yes, this would be considered a regression. The JDK langtools team has filed the following bug report:
JDK-8055963 Inference failure with nested invocation
Judging from the comments, it appears that the current behavior actually conforms to the specification (the JLS), but the behavior is clearly wrong, so it might be the case that a clarification to the specification is necessary.
Note that this is a different type inference bug from the one reported in this other StackOverflow question Java 1.8.20 Compiler Error, bug JDK-8051402. That bug has been fixed already, although but the fix isn't in JDK 8u20.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With