Newsletter |
Difference between String.equals(“value”) and “value”.equals(String) in Java ?
Its very often to use String equals() method in our programming, today i will show you the efficient way of using string equals method. we can write equals() method in two different ways 🙂
- “value from the database”.equals(“string to Compare”)
- “String to compare with”.equals(“value from database”)
The above two syntax’s are correct, then what the difference ? let me explain you with a simple example. I will consider one of the requirements from my previous projects. I have a customer class with getStatus method, which returns some string value (0,1,2) from the database. Lets say,
- 0 – customer not valid
- 1 – customer valid
- 2 – customer blacklisted
My java code would be,
String custStatus = Customer.getStatus();
As per my requirement if the customer is valid then withdraw or deposit logic will be exexuted, if not it will redirect to the home page with some error message saying “customer is not valid“, let me show you programmatically.
According to the First Syntax (“value from the database”.equals(“string to Compare”))
String custStatus = Customer.getStatus(); if(custStatus.equals("1")){ //withdraw logic //deposit logic }else{ //Redirect to homepage with some message "customer is not valid" }
According to the Second Syntax (“String to compare”.equals(“value from database”))
String custStatus = Customer.getStatus(); if("1".equals(custStatus)){ //withdraw logic //deposit logic }else{ //Redirect to homepage with some message "customer is not valid" }
Explanatin
If you consider the first program we are comparing ‘custStatus‘ with “1“. By any chance from the database if we get custStatus value as null rather 0,1,2 then equals method will compare null with “1” which produces NullPointerException. I mean
null.equals("1") // Gives Null pointer exception
But if you consider the second approach, we are comparing “1” with custStatus, even though the custStatus value is null, we will not get any exceptions 🙂 because we are calling the equals() method from String value (“1”). Actually this is a kind of a technique to avoid the NullPointerExceptions, and we can also say its a good coding standard. So totally what i would like to say is, if the constant is always on the left side, then no way it will generate NullPointerException, on that particular equals call, hope you enjoying the article 😉
::. About the Author .:: | ||
very much helpful.
Xcellent!!!
hi …its nice way to deliver every concept,,,I am here to request for JUnit concept, if U have then kindly provide me or post here…
Very nice article!
Found useful..)
Nice One
That’s thoughtfull !
Message should be swapped in the above image. Because it gives wrong direction..
Excellent Questions Provided by you shiva. Great Job!!!!
Nice Example.
Nice Explainaton
Its Very good and help full thanks Sivatej.
Can you please give me a example for how to read data from excel files and how to compare the contents inside the excel two excel files?
I don't see any difference, if we check null before comparing as below: –
if(custStatus != null && custStatus.equals("1")) {
}
if(custStatus != null && "1".equals(custStatus)) {
}
your tutorials are simply awesome!!
Nice Explanation ! keep it up… Share you valuable knowledge with us.