The difference between isBlank() and isEmpty() in Java

Published May 12, 2022

The main difference between these two string operations is this: isBlank() accepts strings with only whitespace while isEmpty() does not.

isBlank() examples

"".isBlank()     // true
"  ".isBlank()   // true
"hi".isBlank()   // false
" hi ".isBlank() // false

isEmpty() examples

"".isEmpty()     // true
"  ".isEmpty()   // false
"hi".isEmpty()   // false
" hi ".isEmpty() // false

StringUtils operates the same way, except it will return true for null inputs (e.g. StringUtils.isEmpty(null) and StringUtils.isBlank(null) are true).