Today I Learned

  • TIL: String.isBlank() vs isEmpty()

    " ".isEmpty() returns false, but " ".isBlank() returns true — whitespace counts as blank, not empty.

    1" ".isEmpty();   // false — length is 1
    2" ".isBlank();   // true  — whitespace only (Java 11+)
    3"abc".isBlank(); // false
    

    I caught myself writing s.trim().isEmpty() again today. String.isBlank() exists since Java 11 and covers exactly this case — plus it is Unicode-aware, so " \u00A0 ".isBlank() also works where trim() misses non-breaking spaces.

↑