"".equals(stringVariable) は改悪だろうã«ã¤ãã¦ã
ã¾ãã空æåã¨ã®æ¯è¼ã¯å ´åã«ãããã¨ãããã空æåã¨ã®æ¯è¼ãªã
s != null && s.isEmpty()
ã
s == null || s.isEmpty()
ãªã©ã¨æ¸ãã»ããããã
ãã¨ã件ã®åé¡ã¯æ¹æªã¨ã¯è¨ããªãã¾ã§ããåé¡æã ããã¿ã¦ãããæ¹åã¨ãããã»ã©ã§ããªãã®ã§ããã¾ãè¯ããªãã
ä¸èº«ããããªãã©ã«ã¨ã®æ¯è¼ã
åºæ¬çãªæ¹éã¨ãã¦ãäºéãã®æ¸ãæ¹ãããã¨ãã¦ãå®è¡ã³ã¹ããè¨è¿°ã³ã¹ããåããªãããå®å
¨ã§å¤æã®å¿
è¦ããªãæ¸ãæ¹ãé¸ã¶ã¹ããã¨ããã®ããããã¨ããããããã¯ã³ã¼ãã®æ¸ãæ¹ã ãã§ã¯ãªãããã¹ã¦ã®è¡åã«ãã¦ã¯ã¾ããå¤æä¸è¦ã§å®å
¨ãªæ¹æ³ãããã®ãªãããããé¸ã¶ã¹ãã ãã·ã£ã¯ã¼ãã¾ã£ããã«ã·ã£ã¯ã¼ç½®ãã«ãããã«æ¨ªåãã«ãããã¨ããã®ã¯ãããã«ãã¦ã¯ã¾ãã確ãã«ã·ã£ã¯ã¼ç½®ãã«ã¯ã·ã£ã¯ã¼ãã¾ã£ããç½®ãããããæ°´æ ãã²ããã¨ãã«æ°´ãã·ã£ã¯ã¼ããã§ããèå£ããåºãã確èªããã°ããã ããã¨ããè¨ãæ¹ãã§ãããã©ããã¯ããã¾ã«ç¢ºèªãå¿ãããããã¦ç¢ºèªãå¿ããã¨ãã«éã£ã¦ã·ã£ã¯ã¼ããæ°´ãåºããã·ã£ã¯ã¼ããå¿
ãã横åãã«ç½®ãããã«ããã°ãå¤æã®å¿
è¦ã¯ãªããæ°´æ ãã²ãã£ã¦æ°´ãæµ´ã³ããã¨ããªãã
ããã¡ãã®ã»ããç¾ãããã¨ããçç±ããããããã¡ãã®ã»ããå®å
¨ãã¨ããçç±ã§æ¸ãæ¹ãé¸ãã ã»ããããããããã£ãç´°ããé¸æãããã°ã©ã ã®ä¿¡é ¼æ§ãããã¦ããã
nullãã§ãã¯ã¯å¥ã«ããã¹ãã ãã¨ãããªããnullãã§ãã¯ã¯å¥ã«ãã¦ãããã«"ãªãã©ã«".equals(s)ã¨ããæ¸ãæ¹ãããã»ããããã
ãNullPointerExceptionãåºãã°ãã«ãã§ãã¯ãããã¹ããã®ãæãã¦ãããã¨ã«æ°ã¥ããã®ã«ãã¨ããæè¦ããã£ããã©ã"ãªãã©ã«".equals(s)ã¨æ¸ããã¨ãã«nullãæ¡ä»¶ããã¯ããã¦å°ããã¨ã¯ã»ã¼ãªããããã«ãããã¯ããã°ã©ã ãæ¸ã人ã®çè«ã§ã使ãå´ããããã°ããã°ã©ã ã®ãã§ãã¯ããããªã¨ããã§ãããã¦ãå°ããéç¨ã«å
¥ã£ããªãå®éã«sãnullã§ãã£ã¦ã¯ãããªãã¨ãã¾ã§NullPointerExceptionã¸ã®ééã¯å
延ã°ãã«ããã¹ãã ã
ããã°ã©ã ã¯ãçµå±åãã¦ãã³ãã§ãåãã®éããæåªå
ã§ãã£ã¦ãè¨è¿°ã®ç¾ãããèªåã¨ã®ããªã·ã¼ã®éãã¯ãç®ãã¤ã¶ã£ãã»ããããã
ããã°ã§ãs.equals("ãªãã©ã«")ã®æ¹ãç¾ããããï¼ãã¨ãããªããããå®éã®ã³ã¼ãã§ã¯ããã¯ãããå®å
¨ãª"ãªãã©ã«".equals(s)ã¨ããæ¸ãæ¹ãããã®ãããã°ã©ãã¨ãã¦è¯ãã¨æãã
ãããã¯ããèªåã¯ã©ããªã«ç²ãã¦ã¦ãæ¥ãã§ãã¦ãç ãã¦ãããæ°ããªãã¦ã確å®ã«nullãã§ãã¯ãè¡ãèªä¿¡ãããããs.equals("ãªãã©ã«")ã¨æ¸ããã諸åãå¡äººã¯"ãªãã©ã«".equals(s)ã¨æ¸ããã¾ããã¨ããã»ããã«ãã³ã¤ã¤ã