Let me start with a sample code first ...
String password = ""; if("PIRATE".equals(password)) {
See here, a constant or a literal String (independently). "PIRATE" is used to check if two lines are equal. While...
String password = ""; if(password.equals("PIRATE")) {
it also works exactly the same as the previous code.
Currently, I see a lot of the first style βSTRING_LITERALβ .equals (STRING_OBJECT) , while Java people are posting code.
So my question is: Where does this style come from? And how much better is this than the second style?
In fact, I think the second style is more logical than the first, why ?!
allows you to fulfill the requirement, for example, if the password provided by the user is "PIRATE", and then give permission to this user
when you start to fulfill the above requirement,
String userProvidedPassword = getPaswordFromUser(); if(userProvidedPassword.equals("PIRATE")) {
Isn't this more logical than "PIRATE" .equals (userProvidedPassword); ?! Just think about it ...
Correct me if I am wrong .. Thanks ..
EDIT: Sorry, this question did not appear in my previous search, and it answers my question perfectly. Also thanks to everyone who helped here.