In Java, which will be more effective, and what are the differences?
if (null == variable)
or
if (variable == null)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(Similar to this question: Difference between null==object and object==null)
I would say that there is absolutely no difference in performance between those two expressions.
Interestingly enough however, the compiled bytecode (as emitted by OpenJDKs javac) looks a bit different for the two cases.
For
boolean b = variable == null:For
boolean b = null == variable:As @Bozho says,
variable == nullis the most common, default and preferred style.For certain situations however, I tend to put the
nullin front. For instance in the following case: