「Java」String型の値のnull判定を行うサンプル
環境
JDK1.8
Eclipse 2019-12
書式
if(文字列 == null || 文字列.length() == 0)
文字列はnullもしくは長さ0かどうかを判定します。
使用例
package com.arkgame.stud;
public class StrIndefo {
      public static void main(String[] args) {
            String strA = "study skill";
            System.out.println("String型の値のnull判定結果1: " + isNullFunc(strA));
            String strB = "";
            System.out.println("String型の値のnull判定結果2: " + isNullFunc(strB));
            String strC = null;
            System.out.println("String型の値のnull判定結果3: " + isNullFunc(strC));
      }
      // 文字列がnullもしくは長さ0かどうか判定
      public static boolean isNullFunc(String str) {
            if (str == null || str.length() == 0) {
                  return true;
            }
            return false;
      }
}
実行結果
String型の値のnull判定結果1: false String型の値のnull判定結果2: true String型の値のnull判定結果3: true