Null vs Empty vs Blank

Sixhustle·2021년 1월 22일
2

Java

목록 보기
9/10

제가 empty와 blank가 매일 헷갈려서 확실하게 짚고 넘어가려고 합니다.
String Validation 확인 중, 자주 사용하는 Null, Empty, Blank의 차이를 알아보겠습니다.
아래의 코드들은 group: org.apache.commons, name: commons-lang3, version: 3.11 입니다.


Null

변수에 아무것도 할당되지(참조하지) 않은 상태를 의미합니다.


Empty

empty(비어있는) : 문자열의 길이가 0임을 의미합니다.

아래는 StringUtils.isEmpty 코드입니다.
눈여겨 보실 점은 """ "의 차이입니다. empty는 length = 0을 의미하기 때문에 " "값은 false로 반환 됩니다.

StringUtils.isEmpty

// Empty checks
//-----------------------------------------------------------------------
/**
 * <p>Checks if a CharSequence is empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 * StringUtils.isEmpty("bob")     = false
 * StringUtils.isEmpty("  bob  ") = false
 * </pre>
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer trims the CharSequence.
 * That functionality is available in isBlank().</p>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is empty or null
 * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
 */
public static boolean isEmpty(final CharSequence cs) {
   return cs == null || cs.length() == 0;
}

Blank

blank(공백) : whitespace(공백, 띄어쓰기)만 있음을 의미합니다.

StringUtils.isBlank

/**
 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
 *
 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace only
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(final CharSequence cs) {
   final int strLen = length(cs);
   if (strLen == 0) {
      return true;
   }
   for (int i = 0; i < strLen; i++) {
      if (!Character.isWhitespace(cs.charAt(i))) {
         return false;
      }
   }
   return true;
}

Run code

아래 5가지의 String Value에 대한 Null, Empty, Blank 결과 값입니다.

결과 값

String ValueNullEmptyBlank
nulltruetruetrue
""falsetruetrue
" "falsefalsetrue
"value"falsefalsefalse
" v a l u e "falsefalsefalse

실행 코드

log.info("{}", null == null);
log.info("{}", "" == null);
log.info("{}", " " == null);
log.info("{}", "value" == null);
log.info("{}", " v a l u e " == null);

log.info("{}", StringUtils.isEmpty(null));
log.info("{}", StringUtils.isEmpty(""));
log.info("{}", StringUtils.isEmpty("  "));
log.info("{}", StringUtils.isEmpty("value"));
log.info("{}", StringUtils.isEmpty(" v a l u e "));

log.info("{}", StringUtils.isBlank(null));
log.info("{}", StringUtils.isBlank(""));
log.info("{}", StringUtils.isBlank("  "));
log.info("{}", StringUtils.isBlank("value"));
log.info("{}", StringUtils.isBlank(" v a l u e "));

Referneces

0개의 댓글