「Java」三項演算子で文字列の値のnull判定をするサンプル

2020年12月3日

書式
条件式?値1:値2
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
public class JavaSange {
protected static boolean result;
public static void main(String[] args) {
String strA = null;
String strB = "arkgame.com";
funcA(strA);
funcA(strB);
}
public static void funcA(String target) {
result = (target == null) ? true : false;
System.out.println("値のnull判定結果: " + result);
}
}
package com.arkgame.study.cft; public class JavaSange { protected static boolean result; public static void main(String[] args) { String strA = null; String strB = "arkgame.com"; funcA(strA); funcA(strB); } public static void funcA(String target) { result = (target == null) ? true : false; System.out.println("値のnull判定結果: " + result); } }
package com.arkgame.study.cft;

public class JavaSange {

      protected static boolean result;

      public static void main(String[] args) {
            String strA = null;
            String strB = "arkgame.com";
            funcA(strA);
            funcA(strB);
      }

      public static void funcA(String target) {
            result = (target == null) ? true : false;
            System.out.println("値のnull判定結果: " + result);
      }
}

実行結果
値のnull判定結果: true
値のnull判定結果: false

Java

Posted by arkgame