Why is generic of a return type erased when there is an unchecked conversion of a method parameter in Java 8?

后端 未结 4 1844
野趣味
野趣味 2020-12-05 13:22

Consider the following code sample:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
             


        
4条回答
  •  春和景丽
    2020-12-05 14:01

    The real question is; why can you use raw type? For backward compatibility. If it's for backward compatibility, the assumption is only raw types should be used.

    How generic type of passed argument could affect generic type of method return value while generic type of return value is fixed in method signature?

    A method or constructor has either of two modes. It is either completely generic, or it is completely raw typed for backward compatibility. There is no mode mode of use where it is partly raw typed and partly generic. The only reason raw types are an option is for backward compatibility and in this situation it assumes all types are/were raw types.

    Why there is such backward incompatible change in behavior between Java7 and Java8?

    Since Java 1.4 is not supported any more and hasn't been for a while, the backward compatibility argument doesn't hold as strongly and make some sense to give raw types a place in the current language.

提交回复
热议问题