开发者

Java instanceof and byte[]

开发者 https://www.devze.com 2023-04-01 00:38 出处:网络
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

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!

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().get开发者_StackOverflow社区SimpleName());
        /* ... handle error gracefully ... */
    }

...and what this outputs is the following:

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

Which means that the object actually was a 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?

What's an appropriate idiom to use here instead?


It looks like you have a ! (not) that you don't need

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

should be

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号