俺はJavaの null・空文字チェックを理解していなかった
仕事でやってていつも何となくやってるなと思い改めて調べてみる
結論
空文字ornull
をtrueとして判定したい場合はStringUtils.isEmpty
が最も良い。
※org.apache.commons.lang3.StringUtils
ライブラリを使用して問題ない環境であれば
String.isEmpty
- 空文字の場合true
- null判定不可(システムエラーとなる)
String.isBlank
- 半角・全角問わず空白スペースの場合true
StringUtils.isEmpty
- 空文字の場合true
- nullの場合true
StringUtils.isBlank
- 半角・全角問わず空白スペースの場合true
- ※今回の検証では
String.isBlank
との違いがわからず・・・
- ※今回の検証では
import org.apache.commons.lang3.StringUtils; import java.util.*; public class Sample { public static void main(String args[]) { String empty = ""; String emptySpace = " "; String emptySpaceFullWidth = " "; String nullVal = null; if (empty.isEmpty()) { System.out.println("isEmpty return true when target is empty"); } else if (empty.isBlank()) { System.out.println("isBlank return true when target is empty"); } if (emptySpace.isEmpty()) { System.out.println("isEmpty return true when target is emptySpace"); } else if (emptySpace.isBlank()) { System.out.println("isBlank return true when target is emptySpace"); } if (emptySpaceFullWidth.isEmpty()) { System.out.println("isEmpty return true when target is emptySpaceFullWidth"); } else if (emptySpaceFullWidth.isBlank()) { System.out.println("isBlank return true when target is emptySpaceFullWidth"); } // システムエラー // if (nullVal.isEmpty()) { // System.out.println("isEmpty3 return true"); // } // else if (nullVal.isBlank()) { // System.out.println("isBlank3 return true"); // } if (StringUtils.isEmpty(nullVal)) { System.out.println("StringUtils.isEmpty return true when target is null"); } else if (StringUtils.isBlank(nullVal)) { System.out.println("StringUtils.isBlank return true when target is null"); } if (StringUtils.isEmpty(empty)) { System.out.println("StringUtils.isEmpty return true when target is empty"); } else if (StringUtils.isBlank(empty)) { System.out.println("StringUtils.isBlank return true when target is empty"); } if (StringUtils.isEmpty(emptySpace)) { System.out.println("StringUtils.isEmpty return true when target is emptySpace"); } else if (StringUtils.isBlank(emptySpace)) { System.out.println("StringUtils.isBlank return true when target is emptySpace"); } if (StringUtils.isEmpty(emptySpaceFullWidth)) { System.out.println("StringUtils.isEmpty return true when target is emptySpaceFullWidth"); } else if (StringUtils.isBlank(emptySpaceFullWidth)) { System.out.println("StringUtils.isBlank return true when target is emptySpaceFullWidth"); } } } // java -cp /Users/nakaokeisuke/pdev/demo/commons-lang3-3.12.0.jar: Sample // javac -classpath commons-lang3-3.12.0.jar Sample.java
結果
isEmpty return true when target is empty isBlank return true when target is emptySpace isBlank return true when target is emptySpaceFullWidth StringUtils.isEmpty return true when target is null StringUtils.isEmpty return true when target is empty StringUtils.isBlank return true when target is emptySpace StringUtils.isBlank return true when target is emptySpaceFullWidth
参考
Java本格入門 ~モダンスタイルによる基礎からオブジェクト指向・実用ライブラリまで 谷本 心 https://www.amazon.co.jp/dp/477418909X/ref=cm_sw_r_tw_dp_41KT0MVGQ0MYMYBMYFXV?_encoding=UTF8&psc=1 @amazonJPより