【问题标题】:Java instanceof and byte[]Java instanceof 和 byte[]
【发布时间】:2011-11-07 00:18:38
【问题描述】:

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

我的条件如下所示:

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

...输出如下:

--- can only encode 'byte[]' message data (got byte[])

这意味着该对象实际上 byte[],但不知何故不是instanceof byte[]。那么......这是否适用于Byte[] 或其他什么?这里到底发生了什么,为什么这没有像我预期的那样工作?

在这里使用什么合适的成语?

【问题讨论】:

    标签: java casting bytearray instanceof


    【解决方案1】:

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

    if (!(potentialByteArray instanceof byte[])) {...}
    

    应该是

    if (potentialByteArray instanceof byte[]) {...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多