Get all updates of Java4s
Follow Us: Email Newsletter

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

Core Java » On Feb 21, 2015 { 14 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,

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”))

According to the Second Syntax (“String to compare”.equals(“value from database”))

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

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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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