What is the purpose of type arguments in constructor call following new?

后端 未结 2 1460
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 05:00

In the Java specification (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9), new has the following form:

ClassInstanceCreationExpressio         


        
2条回答
  •  孤街浪徒
    2021-01-02 05:14

    You can add useless type arguments for method invocations, weird stuff like

    Math.max(1,2);
    
    System.out.

    see http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.1-200-E

    a non-generic method may be potentially applicable to an invocation that supplies explicit type arguments. Indeed, it may turn out to be applicable. In such a case, the type arguments will simply be ignored.

    This rule stems from issues of compatibility and principles of substitutability. Since interfaces or superclasses may be generified independently of their subtypes, we may override a generic method with a non-generic one. However, the overriding (non-generic) method must be applicable to calls to the generic method, including calls that explicitly pass type arguments. Otherwise the subtype would not be substitutable for its generified supertype.

提交回复
热议问题