java 为什么 List<Object> list = new ArrayList<String>() 这会导致 TypeMismatch 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14935394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:55:11  来源:igfitidea点击:

Why List<Object> list = new ArrayList<String>() this give TypeMismatch error

javacollectionsarraylistjava-6

提问by Raghvendra Garg

List<Object> list = new ArrayList<String>()

When I use the above line compiler gives me type mismatch error. But as I understand Object is super class of String and if I create list of object then it should also accept String. So why above statement is wrong. I am looking for an explanation.

当我使用上述行时,编译器给了我类型不匹配错误。但据我所知,Object 是 String 的超类,如果我创建对象列表,那么它也应该接受 String。那么为什么上面的说法是错误的。我正在寻找一个解释。

回答by PermGenError

One sentence, because

一句话,因为

Generic types are not polymorphic

泛型类型不是多态的

i.e., even though java.lang.Stringis a subtype of java.lang.Objectpolymorphism doesn't apply to generic types. It only applies to collection types. thus

即,即使java.lang.Stringjava.lang.Object多态的子类型也不适用于泛型类型。它仅适用于集合类型。因此

List<Object> list = new ArrayList<String>(); //this isn't valid
?   List<Object> list = new ArrayList<Object>(); //valid
List<? extends Object> list = new ArrayList<String>();//valid

Why can't generic types be polymorphic?

为什么泛型类型不能是多态的?

回答by keiki

Because you define what possible list can be associated with.

因为您定义了可以关联的可能列表。

The correct way would be

正确的方法是

List<?> list = new ArrayList<String>();

List<?> list = new ArrayList<String>();

which is the same as

这与

List<? extends Object> list = new ArrayList<String>();

List<? extends Object> list = new ArrayList<String>();

回答by Jiangzhou

Ais a super type of Bdoes not imply List<A>is a super type of List<B>. If such proposition holds, consistency of type system will be violated. Consider the following case:

A是超类型的B并不意味着List<A>是超类型的List<B>。如果这样的命题成立,就会违反类型系统的一致性。考虑以下情况:

// ! This code cannot pass compilation.
List<String> list1 = new ArrayList<String>();
List<Object> list2 = list1;  // Error
// Previous conversion causes type contradiction in following statements.
list2.add(new SomeOtherClass());
String v = list1.get(0);  // list1.get(0) is not String!

That's why List<Object>should not be super type of List<String>.

这就是为什么List<Object>不应该是超类型的List<String>.

回答by Festus Tamakloe

Just replace:

只需更换:

List<Object> list = new ArrayList<String>()

with

List<String> list = new ArrayList<String>()

or

或者

List<Object> list = new ArrayList<Object>()

It is important here that you operate with the same data type

在这里使用相同的数据类型很重要

Or you can also use

或者你也可以使用

 List<?> list = new ArrayList<Object>();

回答by amicngh

The type of the variable declaration mustmatch the type you pass to the actual object type. If you declare List<Foo>foo then whatever you assign to the foo reference MUSTbe of the generic type . Not a subtype of <Foo>. Not a supertypeof <Foo>.

变量声明的类型必须与您传递给实际对象类型的类型匹配。如果你声明 List<Foo>foo ,那么你分配给 foo 引用的任何东西都必须是泛型类型。不是<Foo>. 不是supertype<Foo>

Simple Generic types are not polymorphic

List<Parent> myList = new ArrayList<Child>() \\ Invalid

List<Parent> myList = new ArrayList<Child>() \\ Invalid