You have three options: If the element you are comparing is also known as non-zero (like a constant), then use this first.
if ("hello".equals(variable)) { ... }
Check null first
if (variable != null && variable.equals("hello")) { ... }
Finally, if null and the empty string can be considered the same thread down, then set the string to the empty string. But if you want to handle null differently, you cannot do this.
source share