Recently, a warning was found during encoding. The code is as follows:
List<Future> futureList = new ArrayList<>(2);
Warning: raw use of parameterized class’ future ‘inspection Info: reports any uses of parameterized classes where the type parameters are acknowledged. Such raw uses of parameterized types are valid in Java, but define the purpose of using type parameters, and may mask bugs. This inspection mirrors the rawtypes warning of javac
The original use of the parameterized type “future”. Check information: indicates the use of parameterized classes that omit types. This original use of parameterized types is valid in Java, but it will destroy the purpose of using type parameters and may cover up errors. This check reflects a compile time warning for the original type
Analysis: List & lt; Future>
: parameterized type, indicating that the element type is future list List
: it is the original ecological type corresponding to the parameterized type; The primitive type List
is exactly the same as the interface type before the Java platform has no genericsList< E>
: generic e
: formal type parameter
then why not use the original ecological type
A: using primitive types will lose all the advantages of generics in terms of security and expressiveness
For example:
List list = new ArrayList();
list.add("123");
list.add(1L);
list.add(0.11d);
for (Object o : list) {
double a = (double) o;
}
This code directly uses the implementation of the original ecological type. There will be no problem during the compilation period, but once it runs, it will report an error: exception in thread “main” java.lang.classcastexception: java.lang.string cannot be cast to java.lang.double. The code design should make the error be found as soon as possible, which is an unsafe performance. Moreover, in terms of readability, the, Without formal type parameters, it is difficult for us to understand what the type of list is, and the expressiveness becomes worse
exception
The primitive type must be used in class text, which means that list. Class and int. class are legal, but list
. class is illegal;
It is illegal to use instanceof on parameterized type instead of unrestricted wildcard type, but using unlimited wildcard instead of original ecological type has no effect on instanceof;
It is illegal to create a generic, parameterized type or an array of type parameters;
The primitive type must be used in class text, which means that list. Class and int. class are legal, but list
. class is illegal;
It is illegal to use instanceof on parameterized type instead of unrestricted wildcard type, but using unlimited wildcard instead of original ecological type has no effect on instanceof;
It is illegal to create a generic, parameterized type or an array of type parameters;
These exceptions come from: generic information can be erased at runtime ; Generics exist only in the compilation phase of code, and their element type information is erased at run time. Erasure means that generics can interact with code that does not use generics at will
solve the initial problem
There is no problem with the code itself, and parameterized types are used, but it is worth noting that future is also a generic class. If it is amended as follows, there will be no further warning:
List<Future<ADXParam>> futureList = new ArrayList<>(2);
But when I changed it
This line reports a mistake again. Go deep and see the source code of vertx that is not standardized
static CompositeFuture join(List<Future> futures) {
return CompositeFutureImpl.join(futures.toArray(new Future[futures.size()]));
}
What should we do?First of all, if I am sure that my use is safe and will not cause errors at run time, I can use @ suppresswarnings (“rawtypes”) to eliminate the warning of using primitive ecological types
Scan QR code
Get more
The programmer
is a human being
this article is from WeChat official account – caoyusong1991.
Similar Posts:
- Type safety: unchecked cast from object to
- ArrayList reported an error: Unsupported operationexception
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List
- How to Solve Java Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
- [Solved] lombok Error: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- [Solved] Syntax error, parameterized types are only available if source level is 1.5
- Postman passing Josn field error: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token