简体   繁体   English

条件运算符中的类型不匹配(java)

[英]type-mismatch in a conditional operator (java)

I don't understand why the following would NOT cause an error.我不明白为什么以下内容不会导致错误。

boolean condition = <whatever>;
return condition ? Collections.singleton("a field") : Collections.EMPTY_LIST;

Collections.singleton() returns a java.util.Set which obviously is NOT the same type as EMPTY_LIST . Collections.singleton()返回一个java.util.Set ,这显然与EMPTY_LIST类型不同。

Should that really NOT cause a type-mismatch error?这真的不应该导致类型不匹配错误吗?

Both Collections.singleton() and Collections.EMPTY_LIST implement Collection (and extend Object for that matter). Collections.singleton()Collections.EMPTY_LIST实现Collection (为此扩展了Object )。 No type conversions are necessary to return the result of the ternary operator as a Collection or an Object . 无需类型转换就可以将三元运算符的结果作为CollectionObject

The java compiler finds the most specific type that matches both parts. Java编译器会找到与这两部分都匹配的最具体的类型。 It gets tricky with primitive types, but only void is flatly forbidden. 使用原始类型会很棘手,但是绝对禁止void See the Java Language Specification on the conditional operator . 请参阅条件运算符上的Java语言规范。

Starting point初始点

This code by itself is not the whole picture – it contains a "return" statement, but we do not see the method signature, and thus do not see the return type.这段代码本身并不是全部——它包含一个“return”语句,但我们看不到方法签名,因此看不到返回类型。

boolean condition = true;
return condition ? Collections.singleton("a field") : Collections.EMPTY_LIST;

Method: return type Object方法:返回类型 Object

It would be totally fine to mix object types with a method return type of Object.将 object 类型与方法返回类型 Object 混合使用是完全可以的。 For example, here's a method returning either a HashMap or an Integer.例如,这是一个返回 HashMap 或 Integer 的方法。 This is allowed because the method return type is Object, and both of those return types are Objects.这是允许的,因为方法返回类型是 Object,并且这两个返回类型都是对象。

private static Object returnObject() {
    boolean condition = true;
    return condition ? new HashMap<>() : Integer.valueOf(1);
}

You could put the original code in a method with return type Object, and the compiler would be fine with it.您可以将原始代码放入返回类型为 Object 的方法中,编译器就可以了。


Actual object types实际 object 类型

The type of Collections.singleton("a field") is Set<String> , and Set has the following definition, showing it extends Collection: Collections.singleton("a field")的类型是Set<String> ,并且Set具有以下定义,表明它扩展了 Collection:

public interface Set<E> extends Collection<E>

The type of Collections.EMPTY_LIST is List which has this definition, showing it extends Collection, too: Collections.EMPTY_LIST的类型是具有此定义的List ,表明它也扩展了 Collection:

public interface List<E> extends Collection<E>

Both the set and the list share a common parent: Collection.集合和列表共享一个共同的父对象:集合。


Method: return type Collection方法:返回类型集合

If you put the original lines of code along with a method signature having return type "Collection", that would be allowed by the compiler since both possible return values are Collections.如果将原始代码行与返回类型为“Collection”的方法签名放在一起,编译器将允许这样做,因为两个可能的返回值都是 Collections。

private static Collection returnCollection() {
    boolean condition = true;
    return condition ? Collections.singleton("a field") : Collections.EMPTY_LIST;
}

Third option: Iterable第三个选项:可迭代

As shown above, the method can have a return type of either Collection or Object.如上所示,该方法的返回类型可以是 Collection 或 Object。 Additionally, you could specify the return type as Iterable , since Iterable is the parent of Collection .此外,您可以将返回类型指定为Iterable ,因为 Iterable 是Collection的父级。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM