Simple question, which is correct way of using equals, also provide reasoning behind answer.
"Delta".equals(type)
or
type.equals("Delta")
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.
Generally
is favored, as it is impossible to throw a
NullPointerException. That said, the other way is not “incorrect” as it is not in error with the Java Language Specification; however, it is just susceptible to failure if(type == null)is true.The term “best practice” is used to differentiate a better choice from a correct, but inferior choice. In this case
"Delta".equals(type)is a best practice, to avoid the unnecessary guard code required to handle null pointer references.