Home » Tip Bank » Java
  • Email Article
  • Comment on this Article
  • Share Article
    • Digg
    • del.icio.us
    • Newvine
    • furl
    • StumbleUpon
    • BlinkList
    • Newsvine
    • Magnolia
    • Facebook
    • Tailrank
    • Slashdot
    • Technorati
    • Google Bookmarks
    • Yahoo Favorites
    • Windows Live
    • Ask
Language: Java
Expertise: Intermediate
Jan 2, 2018

WEBINAR: On-Demand

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Learn Some Efficient Programming Practices

Avoiding null pointer exceptions is a good programming practice. However, simple things such as string comparison can also lead to null pointer exceptions.

Considering the following:

You have a constant string and a variable string to be compared. There are 2 ways of checking their equality, however the variable string can be null in both the cases.

Case 1: variableString.equals(constantString)
Case 2: constantString.equals(variableString)

In the above cases, case 1 can lead to a null pointer exception if the variableString is null. However, in case 2 it does not lead to a null pointer exception, thereby your program becomes safer. Please consider such recommendations while writing your code.

M S Sridhar
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
htmlandcssbook.com
enable-javascript.com