Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Learn Some Efficient Programming Practices

Avoiding null pointer exceptions is a good programming practice. Learn some more ways to avoid this pitfall.


advertisement

WEBINAR:

On-Demand

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


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.

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date