简体   繁体   English

我怎样才能让这个 Java instanceof 测试返回 false 而不是错误?

[英]How can I get this Java instanceof test to return false rather than an error?

I have been experimenting with instanceof in my Java code.我一直在我的 Java 代码中试验 instanceof。

public class Test {
    public static void main(String[] args) {
        String favoriteFood = "sandwich";
        boolean flag = favoriteFood instanceof StringBuilder; //incompatible types
    }
}

I get an error from flag because String cannot be cast to StringBuilder.我从 flag 收到错误,因为 String 无法转换为 StringBuilder。 I understand that testing a String to check if it is an instance of StringBuilder is illogical because Strings can never be cast as StringBuilders.我知道测试 String 以检查它是否是 StringBuilder 的实例是不合逻辑的,因为 Strings 永远不能被转换为 StringBuilders。 I also understand the benefits of failing fast.我也理解快速失败的好处。 However, is there any way to still run this test and return false?但是,有没有办法仍然运行此测试并返回 false? I have looked at related posts, but they have more to do with why there is an error than what to do about it.我看过相关的帖子,但它们更多地与错误的原因有关,而不是如何处理。

(I know there is no practical need to do this. I just want to know if this can be done. If yes, then how? If not, then why not?) (我知道没有实际需要这样做。我只是想知道这是否可以做到。如果是,那怎么做?如果不是,那为什么不呢?)

The simplest approach is:最简单的方法是:

boolean flag = false;

but if that's unappealing to you for some reason, you can write:但如果由于某种原因这对你没有吸引力,你可以写:

boolean flag = ((Object)favoriteFood) instanceof StringBuilder; // false

and then bribe a coworker to let it past code review.然后贿赂同事让它通过代码审查。

The answer to If not, then why not? If not, then why not? is "because the language spec says so" (see comment by Carlos H.).是“因为语言规范是这样说的”(参见 Carlos H. 的评论)。

And the answer to "why does the language spec say so" is that language definers have this tendency to outlaw constructs that make no sense whenever they can, and this tendency is inspired by their belief that in doing that, they are helping you to write better code.而“为什么语言规范这么说”的答案是语言定义者有这种倾向,只要他们可以,就禁止毫无意义的结构,这种倾向受到他们的信念的启发,即这样做,他们是在帮助你编写更好的代码。

EDIT编辑

re.关于。 "does defining a boolean as (2 + 2 == 5) make any more logical sense than ..." : no, it doesn't, but : “将布尔值定义为 (2 + 2 == 5) 是否比...更具逻辑意义”:不,它没有,但是:

(a) it is impossible (that is, logistically infeasible) for language definers to inventorize all the things that could be written but make no logical sense (*) (a) 语言定义者不可能(即逻辑上不可行)盘点所有可以编写但没有逻辑意义的东西 (*)
(b) this kind of problem boils down to proving the emptiness of a set (eg proving that the set of all possible instances of String that are also instances of StringBuilder is empty) and proving the emptiness of a set in general as a problem is NP-hard. (b) 这种问题归结为证明一个集合的空性(例如证明所有可能的 String 实例的集合也是 StringBuilder 的实例是空的)并且证明一个集合的空性一般是一个问题NP难。 Given specific extra information, it might be possible and is sometimes done, eg given a type hierarchy that almost literally says "no String can also be a StringBuilder".给定特定的额外信息,这可能是可能的,有时也是这样做的,例如,给定一个类型层次结构,几乎字面意思是“没有 String 也可以是 StringBuilder”。 But in general , that is undoable.总的来说,这是可以撤销的。 Which is why you'll always find cases if only you keep searching hard enough.这就是为什么只要你足够努力地搜索,你总能找到案例。

(*) For your sense of "logical sense", but what you (/we all) really mean, is really just "good programming practice", one of which would be "avoiding obfuscated ways of writing false ". (*) 对于的“逻辑感”,但您(/我们所有人)真正的意思是,实际上只是“良好的编程实践”,其中之一是“避免编写false混淆方式”。 It might seem counterintuitive to you, but logic has no concept of "logical sense".这对您来说可能看起来违反直觉,但逻辑没有“逻辑意义”的概念。 2+2==5 is just another logical proposition, and it happens to be false. 2+2==5只是另一个逻辑命题,它恰好是错误的。 Logic does not complain about falsehoods, it just observes them.逻辑不会抱怨谎言,它只是观察它们。

(PS I know I used "makes no sense" too, shouldn't have, but I allowed myself to get carried away.) (PS 我知道我也用了“毫无意义”,不应该,但我让自己得意忘形。)

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

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