Java logical operator (&&, ||) short-circuit mechanism

后端 未结 6 2001
野的像风
野的像风 2021-02-19 06:28

As I was reading a colleague\'s Java code, I stumbled upon an army of if/else statements. In these statements, several && and || operators wer

6条回答
  •  隐瞒了意图╮
    2021-02-19 06:56

    I see what you were trying to do, but before operating on parentheses it's good to have the language's specification handy. I've known a few programmers who have gone so far as to keep the important bits, like operator precedence, on their walls.

    The second part is to know whether your alterations are going to make any sense to the other people in your workplace; if removing parentheses compromises the meaning of an operation to someone else working on the code, then it could ultimately be detrimental.

    The optimizer will generally take care of it for you, so when in doubt, leave them in there.

提交回复
热议问题