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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: