简体   繁体   English

Java explicit casting on list.get(0) (incompatible types: java.lang.Object cannot be converted to java.lang.String)

[英]Java explicit casting on list.get(0) (incompatible types: java.lang.Object cannot be converted to java.lang.String)

The following code is mentioned in the Java Tutorials:-( https://docs.oracle.com/javase/tutorial/java/generics/why.html ) The following code is mentioned in the Java Tutorials:-( https://docs.oracle.com/javase/tutorial/java/generics/why.html )

List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);

The casting in the last line suggests that list.get(0) does not return a String.最后一行的转换表明 list.get(0) 不返回字符串。 Now, since list was not declared with any data type, it can take in any object.现在,由于列表没有用任何数据类型声明,它可以接受任何 object。 So I assumed that list.get(0) returns an Object type.所以我假设 list.get(0) 返回一个 Object 类型。

But when I check list.get(0) instanceof String --> it returns true.但是当我检查list.get(0) instanceof String --> 它返回 true。

Why is then the explicit casting required.那么为什么需要显式转换。 Please explain.请解释。

So I assumed that list.get(0) returns an Object type.所以我假设 list.get(0) 返回一个 Object 类型。

Yes it does.是的,它确实。

But when I check list.get(0) instanceof String --> it returns true.但是当我检查 list.get(0) instanceof String --> 它返回 true。

Yes, it should return true because the item at index 0 of the list is a String .是的,它应该返回true ,因为list索引0处的项目String

Why is then the explicit casting required.那么为什么需要显式转换。

it's required simply because the list contains Object s and not every Object from the list is guaranteed to be a String .它是必需的,因为列表包含Object s,并不是列表中的每个Object都保证是String The explicit cast is required so that you can assign the reference of list.get(0) to s (a variable of type String ).显式转换是必需的,以便您可以将list.get(0)的引用分配给sString类型的变量)。

The casting in the last line suggests that list.get(0) does not return a String.最后一行的转换表明 list.get(0) 不返回字符串。

That's inaccurate.这是不准确的。 If the returned object wasn't a String , then casting wouldn't have been possible.如果返回的 object不是String ,则无法进行强制转换。 In other words, you can only cast something to String if that something is a String .换句话说,您只能将某些东西转换为String如果该东西是String Try casting something else, you'll see:尝试投射其他东西,你会看到:

Object i = Integer.valueOf(5);
String j = (String)i; //Exception!

The cast is needed in your code because list.get(0) returns an Object type, but we want to assign it to a String s .您的代码中需要强制转换,因为list.get(0)返回Object类型,但我们想将其分配给String s We as the programmers know that the Object returned from the list is in fact a String , but the compiler doesn't.作为程序员,我们知道从列表返回的Object实际上是String ,但编译器不是。 The compiler just sees this:编译器只看到这个:

Object l = list.get(0);
String s = l;

The second line cannot compile without an explicit cast to String .如果没有显式转换为String ,第二行将无法编译。

And just for clarification.. you'll never see such code in the real world unless you're working on something seriously old.只是为了澄清......你永远不会在现实世界中看到这样的代码,除非你正在处理一些非常古老的东西。 Java has generics since Java 5 (2004), so 15 years now. Java 自 Java 5 (2004) 以来具有 generics,所以现在已经 15 年了。 At this point you really shouldn't see List without it being List<SomeType> (eg List<String> )在这一点上,你真的不应该看到没有List List<SomeType>的 List (例如List<String>

The List list = new ArrayList(); List list = new ArrayList(); is an unparameterized type (which you want to avoid), meaning it can take any Object .是一个未参数化的类型(你想避免),这意味着它可以采用任何Object

Since your list contains any Object s, list.get(0);由于您的list包含任何Objectlist.get(0); can only know that it's returning an Object and doesn't know that the element at 0 is actually a String .只能知道它正在返回一个Object并且不知道 0 处的元素实际上是一个String Since it doesn't know that it's really a String the compiler warns you that you're doing an unchecked cast from Object to String .由于它不知道它确实是一个String ,因此编译器会警告您正在执行从ObjectString的未经检查的强制转换。

list.get(0) instanceof String returns true because it's a runtime check that the element at 0 is in fact a String . list.get(0) instanceof String返回 true 因为它是运行时检查 0 处的元素实际上是String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不兼容的类型:java.lang.Object无法转换为java.lang.String - incompatible types: java.lang.Object cannot be converted to java.lang.String 不兼容的类型:java.lang.Object无法转换为java.lang.String吗? - incompatible types: java.lang.Object cannot be converted to java.lang.String? java.lang.Object []无法转换为java.lang.String [] - java.lang.Object[] cannot be converted to java.lang.String[] 不兼容的类型:java.lang.Object无法转换为T - incompatible types : java.lang.Object cannot be converted to T 错误:类型不兼容:java.lang.Object 无法转换为 E - Error: incompatible types: java.lang.Object cannot be converted to E JAVA:不兼容的类型:int 无法转换为 java.lang.String - JAVA: Incompatible types: int cannot be converted to java.lang.String Java:不兼容的类型:java.lang.String 不能转换为 int - Java:incompatible types: java.lang.String cannot be converted to int java:不兼容的类型:T 无法转换为 java.lang.String - java: incompatible types: T cannot be converted to java.lang.String 错误:不兼容的类型:java.lang.String 无法转换为 String - error: incompatible types: java.lang.String cannot be converted to String 不兼容的类型:卡无法转换为java.lang.String - Incompatible types: Card cannot be converted to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM