java - Why does this compile in Java7 and does not in Java8? -


Generic are difficult and it seems that they are treated differently in different versions of Java.

This code is compiled successfully in Java 7 and fails to compile with Java 8.

  import java.util EnumSet; Public Sector Main {Public Stable Zero Main (String [] Args) {Enum foo = null; TryCompile (EnumSet.of (foo)); } Fixed & lt; C Enum & lt; C & gt; & Amp; One more & gt; Void trycompile (iterable & lt; c & gt; i) {} Static Interface Other {}}   

Here is an error message from Java 8. I used it to compile it:

  /tmp/java_A7GNRg/Main.java:6: error: try the method not to be applied to the main given types in the compilation class can; TryCompile (EnumSet.of (foo)); ^ Required: Iterable & lt; C & gt; Found: EnumSet Reason: Estimated type does not conform to upper bound (s): Enum Upper bounds: Enum & lt; Enum & gt; Another where there is a type-variable: C Enum & lt; C & gt; Expanded, other declared in law & lt; C & gt; Simple (lt; c & gt;) /tmp/java_A7GNRg/Main.java:6: WARNING: [uncontrolled] Call of unchecked method: Method in the class EnumSet is applied to the given types Complex (EnumSet.of (foo)); ^ Expected: E found: Enum where E is a type-variable: E Enum & lt; E & gt;   

The method is about the difference between Java compiler versions.

The main difference between Java 7 and Java 8 is the target type estimate. While the method of determining the arguments of the Java 7 type method, one method only considers the allocation criteria, Java 8 will use the target type of an expression, which means the type of parameter in the case of the nested method, the type of variable that starts Or is specified in the Return statement, or the return type of the method.

Example when writing, list & lt; Number & gt; List = arrays.asList (1, 2, 3, 4); , looking at the Java 7 method, the list for the right side & lt; Integer & gt; will estimate the logic of S. and the target type of Java 8 list & lt; Number & gt; , to understand the argument that the method argument should have an example of number which is the case. Therefore, it is legal in Java 8.

If you are interested in formal details, then you can study exclusively, however, this is not an easy read. |

What happens when you say Enum foo = null; TryCompile (EnumSet.of (foo)); ?

Expression In EnumSet.of (foo) type 7 will be estimated by looking at the type of argument, foo which is the raw type Enum , so an uncheck operation will be performed and the result type of type is EnumSet . This type implements the raw type interactive and therefore it can be sent to tryCompile for another uncontrolled operation.

The target type of 8 in Java is the first parameter of type EnumSet.of (foo) tryCompile which is Iterable < C Enum & lt; C & gt; & Amp; One more & gt; , so without knowing too much in the description, EnumSet.of will be treated as a raw type call in Java 7 because it has a raw type argument of Java 8, its Treatment will be treated as a general orientation because it has a normal target type, by treating it as a general orientation, the compiler will be concluded that the type found ( Enum ) is required type C Enum & Lt; C & gt; & Amp; Another . When you type the raw type Enum to C. Enum < C & gt; If you can participate with specifying with an uncontrolled warning, it will be treated as incompatible with other (without a type-cast).

You can actually include such artists:

  Enum foo = null; TryCompile (EnumSet.of ((Enum & another) foo));   

This must be from Enum to ca Extended Enum , it collects without unwanted warning.

You can also break the target type relationship so that the same steps like Java 7 can be executed:

  Enum foo = null; EnumSet set = EnumSet.of (foo); tryCompile (set); Here, the raw type is used in all three rows, so it compiles with unwanted warnings and applies the same ignorance about  as  That's in Java 7.   

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -