The second version ( null == str
) is called a yoda condition.
They both result in the same behavior, but the second one has one advantage: It prevents you from accidentally changing a variable, when you forget one =
. In that case the compiler returns an error at that row and you're not left with some weird behavior of your code and the resulting debugging.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…