Java instanceof 和 byte[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7247198/
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
Java instanceof and byte[]
提问by Joseph Weissman
What I would expect is that 'potentialByteArray instanceof byte[]
would return true when potentialByteArray
is an instance of a byte[]
, but this doesn't seem to happen -- it's always false for some reason!
我期望的是 'potentialByteArray instanceof byte[]
会在potentialByteArray
是 a 的实例时返回 true byte[]
,但这似乎不会发生 - 由于某种原因它总是错误的!
I've got a conditional that looks like the following:
我有一个如下所示的条件:
if (!(potentialByteArray instanceof byte[])) { /* ... process ... */ }
else {
log.warn("--- can only encode 'byte[]' message data (got {})", msg.getClass().getSimpleName());
/* ... handle error gracefully ... */
}
...and what this outputs is the following:
...这输出的是以下内容:
--- can only encode 'byte[]' message data (got byte[])
Which means that the object actually wasa byte[]
but wasn't an instanceof byte[]
somehow. So... would this work for Byte[]
instead or something? What's really going on here, and why isn't this working as I am expecting?
这意味着该对象实际上是一个byte[]
但不是instanceof byte[]
不知何故。所以......这会起作用Byte[]
吗?这里到底发生了什么,为什么这不像我期望的那样工作?
What's an appropriate idiom to use here instead?
在这里使用什么合适的成语?
回答by Bala R
It looks like you have a !
(not) that you don't need
看起来你有一个!
你不需要的(不是)
if (!(potentialByteArray instanceof byte[])) {...}
should be
应该
if (potentialByteArray instanceof byte[]) {...}