Java instanceof和byte []

Jos*_*man 4 java casting bytearray instanceof

我所期待的是' potentialByteArray instanceof byte[]当a potentialByteArray是a的实例时会返回true byte[],但这似乎不会发生 - 由于某种原因它总是错误的!

我有条件,如下所示:

if (!(potentialByteArray instanceof byte[])) { /* ... process ... */ }
else  {
        log.warn("--- can only encode 'byte[]' message data (got {})", msg.getClass().getSimpleName());
        /* ... handle error gracefully ... */
    }
Run Code Online (Sandbox Code Playgroud)

......以及以下内容如下:

--- can only encode 'byte[]' message data (got byte[])
Run Code Online (Sandbox Code Playgroud)

这意味着该对象实际上一个byte[]但不是instanceof byte[]某种方式.那么......这会起作用Byte[]吗?这里到底发生了什么,为什么这不像我期待的那样有效?

在这里使用什么是合适的习惯用语?

Bal*_*a R 13

看起来你有一个!(不)你不需要的东西

if (!(potentialByteArray instanceof byte[])) {...}
Run Code Online (Sandbox Code Playgroud)

应该

if (potentialByteArray instanceof byte[]) {...}
Run Code Online (Sandbox Code Playgroud)

  • 呻吟.谢谢 :) (2认同)