I was asked this question in interview. Which of the following is better to use
 MyInput.equals("Something");      Or
"Something".equals(MyInput);   Thanks
I would go for
"Something".equals(MyInput);   in this case if MyInput is null then it won't throw NullPointerException
Here we are sure that the object on which equals() is going to invoke is NOT NULL. 
And if you expect NullPointerException from your code to take some decision or throw/wrap it, then go for first.
There is no performance impact
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With