I saw some people wrote this Boolean equal in their code, I usually put constant at the right hand side of "==" operator. I notice 0 == a has faster operation than a == 0. Can someone explain why? And what's the best practice for it?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
if(0==a){ … } is same as if(a==0){ … }. The code will always run when the set condition is true. It is only logical / psychological to place variable value "a" before the constant "0". that is if(a==0){ … }.
Putting a constant on the left side of a comparison protects you against testing the result of an assignment (in C).
In java it is very much less likely, as
is not legal, a not being a boolean.
Can you provide a benchmark for the statement that
0==a
is faster thana==0
. I just viewed the bytecode for two files that differed only in their use of their choice of whether the constant was at the left of right of the==
and found no difference to the bytecode.Many people choose to put the constant on the left side to ensure that an accidental use of the assignment operator
=
instead of the comparison operator==
gets caught by the compiler. It's less of an issue in Java since unless the value ofa
is a boolean this won't typecheck.As other answers have described, the convention of putting constants to the left of
==
is to prevent programmer mistakes. It's called Yoda conditions (I'll expand on this at the end of the answer).You talk about performance, so I suggest you view the bytecode:
becomes:
and
becomes:
In other words, the two are absolutely identical at the bytecode level, so there can't be a performance difference between them.
So it really comes down to readability. The principal purpose of conditions of the form
int == var
is to avoid mistakes likevar = int
(notice the single=
), which would not test for equality but rather assign tovar
and return the value of the assignment. The value of the assignment is an integer, which in many programming languages can be used in a boolean context. In Java, however, an integer cannot be used in a boolean context, so you don't need to worry about mistakenly typing a single-equal instead of a double-equal, since it will be flagged by the compiler as an error. Therefore, you should prefera == 0
in Java, and not the reverse which is slightly more convoluted and harder to read.In fact, the Wikipedia article I linked to makes this statement:
It's a relic of the C/C++ world.
In C, the advantage of writing
0 == a
vs.a == 0
is that you can't accidentally writea = 0
instead, which means something entirely different. Since0
is an rvalue,0 = a
is illegal.In Java that reasoning does not apply because
a = 0
is illegal as well (since0
is not boolean,a
can't be boolean). It doesn't hurt though, so it really doesn't matter a lot which one to choose.Performance has absolutely nothing to do with that.
a == 0
and0 == a
has the same speed.==
with=
becausea = 0
compiles in certain programming languages inside the if/while/.. statements and result incorrect behavior of the program