Newsletter

Difference between String.equals(“value”) and “value”.equals(String) in Java ?

Core Java » on Feb 21, 2015 { 16 Comments } By Sivateja

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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

16 Responses to “Difference between String.equals(“value”) and “value”.equals(String) in Java ?”
  1. irshad says:

    very much helpful.

  2. seshagiri rao says:

    Xcellent!!!

  3. bikesh says:

    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…

  4. Vijay says:

    Very nice article!

  5. Vivek says:

    Found useful..)

  6. SOMA SEKHAR says:

    Nice One

  7. sarronya says:

    That’s thoughtfull !

  8. Prakash says:

    Message should be swapped in the above image. Because it gives wrong direction..

  9. Yogesh Sanas says:

    Excellent Questions Provided by you shiva. Great Job!!!!

  10. Govindsinh says:

    Nice Example.

  11. Haribabu says:

    Nice Explainaton

  12. Its Very good and help full thanks Sivatej.

  13. rijesh says:

    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?

  14. Santosh says:

    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)) {
    }

  15. priyank says:

    your tutorials are simply awesome!!

  16. Chetan says:

    Nice Explanation ! keep it up… Share you valuable knowledge with us.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.