EffectiveJava -- do not use the original ecological type in the code.
Let's take a look at a chestnut and see if we can find out the error:
/*** Do not use the original ecological type in the new Code * @ author weishiyao **/public class Test {public static void main (String [] args) {List <String> strings = new ArrayList <> (); unsafeAdd (strings, new Integer (42); String string = strings. get (0); System. out. println (string);} private static void unsafeAdd (List list, Object object) {list. add (object );}}
Run the command to check the error result:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Stringat com.weishiyao.effectiveJava.entry23.Test.main(Test.java:16)
Yes, it is a ClassCastException, which is a conversion claimed by the compiler, so it is generally successful, but I ignored a warning from the compiler in this example, it will pay for it.
However, if unsafeAdd (List <Object> list, Object object) is used instead of the native List type, our IDE will directly report an error.
If you are not sure or do not care about the element type in the set, you may use the original element type. For example, if you want to write a method, it has two sets and returns the number of their public elements. If you are not familiar with the model, you can refer to the following methods to compile this method:
static int numElementsInCommen(Set s1, Set s2) {int result = 0;for (Object object : s1) {if (s2.contains(object)) {result++;}}return result;}
This method is feasible, but it uses the original ecological type, which is very dangerous. Java has provided a safe alternative from the release of java, called an unrestricted wildcard type. If you want to use a fan type, but are sure or do not care about the actual parameter type, you can use a question mark instead. For example, the unrestricted wildcard type of the fan type Set <E> is Set <?>. This is the most common parameterized Set type and can hold any Set.
In the unrestricted wildcard type Set <?> What is the difference between it and the original Set? Does this question mark really play a role? This does not need to be described in detail, but the wildcard type is safe, and the original type is not safe. Any element can be placed in a Collection that uses the original ecological type, so it is easy to break the constraints of the Collection type. However, no element (except null) can be placed in the Collection. <?> .
Do not use the original ecological type in the new Code. This rule has two small exceptions, both of which are due to the fact that "fan information can be erased at runtime. Native form must be used in class text. Parameterization types are not allowed in the specification. In other words, both List. class, String []. class and int. class are valid. But List <String>. class and List <?> Is invalid.
The second exception of this rule is related to the instanceof operator. Because the fan type information can be erased at run time, It is invalid to use the instanceof operator on parameterized types instead of unrestricted wildcards at one time. Replace the original type with an unrestricted wildcard type, which does not affect the behavior of the instanceof operator. In this case, the angle brackets <> and question mark? It seems redundant. The following is the preferred method for using the model to use the instanceof OPERATOR:
if (object instanceof Set) {// Raw typeSet<?> m = (Set<?>) object;// Wildcard type}
Note: once this object is determined to be a Set, it must be converted to a wildcard type Set <?>, Instead of the native type, this is checked. Otherwise, our IDE will still report an error.