What's the purpose to bit-shift int value by zero?

前端 未结 3 1199
悲哀的现实
悲哀的现实 2021-01-17 14:09

Looking to the source code of java.nio.DirectByteBuffer class, I have found this:

if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) ....
         


        
3条回答
  •  感情败类
    2021-01-17 14:35

    The i << 0 is plainly redundant. There is no good reason for a Java programmer to write this code deliberately.

    I'd say that this code is:

    • written by someone who wasn't thinking,
    • written by someone who doesn't understand what the << operator does,
    • the result of some semi-mechanical refactoring, or
    • originally produced by some sort of code generator or translator.

    However, there's a good chance that the bytecode or JIT compiler will optimize this away, or that it won't impact significantly on performance anyway.

提交回复
热议问题