Please help me how does the string.equals in java work with null value? Is there some problem with exceptions? Three cases:
boolean result1,result2, result3;
You cannot use the dereference (dot, '.') operator to access instance variables or call methods on an instance if that instance is null
. Doing so will yield a NullPointerException
.
It is common practice to use something you know to be non-null for string comparison. For example, "something".equals(stringThatMayBeNull)
.
To prevent NPE while comparing Strings if at least one of them can be null, use StringUtils.equals method which is null-safe.