JAVA: Check If A String is NULL or Empty

  • Post author:
  • Post category:JAVA / String

Checking or verifying that a string is null or empty is one of very basic string operations. It’s just a one line of code and the job is done!

if(str != null && !str.isEmpty()) 
{ /* do your stuffs here */ }
Please note that the first part of '&&' operator is used correctly. JAVA will proceed to evaluate the second part in case the first part has any issues. This will also not throw the null pointer exception for empty strings.

Check other JAVA related posts here:

JAVA Code Snippets