å ç¢ãªã³ã¼ãã£ã³ã°ã«ã¼ã«ãçå®ããæ¹æ³(1)
ãããå¦çãããã³ã¼ããæ¸ããã¨ããã®ã¯ãããåºæ¥äºãæç« ã«è¡¨ç¾ãããã®ã¨ä¼¼ã¦ããã¨æã£ã¦ããã
ã¾ã表ç¾ã«ä½¿ãè¨èªã¯æ§ã ã§ããã使ç¨äººå£ã®å¤§å°ã¯ããã©ãåºæ¬çã«åªå£ã¯ãªããè¨èªãåãã ã£ãã¨ãã¦ããå人åè²ã®è¡¨ç¾ãããå ¨ãåãåºæ¥äºã表ç¾ãããã®ã§ãã£ã¦ããè¤æ°äººãå ¨ãåãæç« ãä½ãå¯è½æ§ã¯éããªãå°ãããåã人ã表ç¾ãããã§ãã£ã¦ããä»ã¨æã§ãå ¨ãåãæç« ããæ¸ãã¨ã¯éããªãããããå¾®å¦ã«å·®ç°ãããã®ãæ®éã ã
ãããªãã³ã¼ãã£ã³ã°ãã«ä¸è²«æ§ãæãããã®ãã³ã¼ãã£ã³ã°ã«ã¼ã«ã ãã¤ã³ãã³ãã¯ã¹ãã¼ã¹4ã¤ã ã¨ããbraceã®ä½ç½®ã¯ã©ãã ã¨ããè¤æ°äººã§å°èª¬ãå·çãã*1å ´åã¯ããã§ãã¾ã調ã«çµ±ä¸ããããã¨ãè²ã ãªåã決ããããã¨æãããã®ãããã®ä¸è²«æ§ããªãã¨ãé常ã«èªã¿ã«ããå°èª¬ãåºæ¥ä¸ããã ããã
ã¨ã¾ããã«ã¼ã«ã£ã¦ã®ã¯ãè¤æ°äººéã®æèåããã®ããã«*2åå¨ãããä¾ãã°æ³å¾ãããã ãããã¦ã誰ããè¦ã¦ãåãå¤æãã§ãããã¨ãæã¾ãã*3ã
ä¾ãã°ãã³ã¼ãã¬ãã¥ã¼ãããã«ãã¦ãããããææãã¹ããå¦ããã¯ã«ã¼ã«ã«ä¾åãããä¾ãã°ã«ã¼ã«ããåèªãæ°æã¡è¯ãããã«æ¸ãã¹ããã ã£ããã°ãææãã¹ããå¦ãã¯å¤æãã¤ããªããã¾ãããã®äººã¯æ°æã¡ããã£ããã ãããªãããããããããããæ°æã¡æªãã¨æãã¤ã¤ãæ¸ãé²ãã¦ãã¾ã£ãã®ãï¼ â¦â¦ã¨ã³ã¸ãã¢ã®æéã¯è²´éã§ããããããªãã¨ã«é ãæ©ã¾ãããªãã¦å¿ä½ç¡ãã
ãã£ã¨èãã¦ã¿ãã®ã ããã«ã¼ã«ã¯ä»¥ä¸ã®ãããªæ§è³ªãæã¤ã¹ãã§ããã¨èãã¦ããã
- ã«ã¼ã«ã«åè´ããããé¸è±ãã¦ããããæ確ã§ãã
- ãã¤ãã©ãã§ãã ããå¤æãã¦ããã«ã¼ã«ãå¤ãããªãéãåãçããåºãï¼ç°å¢ã«ä¾åããªãï¼
- çµè«ãåºãããã»ã¹ãã©ã¤ãã¦ã§ã¤ãã§ãã
- è¤æ°äººãéã¾ã£ã¦è°è«ããªãã¨çµè«ãåºãªããã¨ãã¯ããã¼ã¦ã§ã¤ãã§ããã
- 大éã®ææ¸ãèªãã ããè¤éãªå¤æããã»ã¹ãçµãªãã¨çµè«ãåºããªããã¨ãã¯ããã¼ã§ããã
以ä¸ã®ãããªæ§è³ªãæã¤ã«ã¼ã«ãä½ãã«ã¯ãã©ããããããã ãããï¼ Jiemamyã§ã¯ãåºæ¥ãéãä¸è¨ã®æéã«å¾ã£ãã«ã¼ã«ãçå®ããæ¡ç¨ãããã¨åªãã¦ãããã¾ãã¯ä»¥ä¸ã®åãã«çãããããããªãã¸ãã¯ãä½ã£ã¦ãããããã
ãããã³ã¼ãããã°ã§ããã¨å¤æããæé ã¯ã©ããªãã®ãï¼ãã¤ã¾ãããã°ã¨ã¯ä½ãï¼ã*4ã§ããã
public class Foo { private int bar; public String getBar() { return String.valueOf(bar); } public void setBar(String bar) { this.bar = Integer.parseInt(bar).intValue(); } } public class Main { public static void main(String[] args) { Foo foo = new Foo(); foo.setBar("hoge"); System.out.println(foo.getBar()); } }
ãã®ã³ã¼ãã¯ãå®è¡ããã¨NumberFormatExceptionã§ããã»ã¹ãçµäºãã訳ã§ããããã®ã³ã¼ãã«ãã°ã¯ããã ãããï¼ ããã¨ããããã©ãããã°ãªã®ã ãããï¼ å¯è½æ§ãèãã¦ã¿ãã
- Fooã®ãã°èª¬
- Foo#setBar()ã§ã¯ããããStringãå¼æ°ã§æ¸¡ããã¹ãã§ãããhogeãåãä»ããªãFooãæªã§ããã
- Mainã®ãã°èª¬
- Foo#setBar()ã«ã¯ãIntegerã®ç¯å²ã®æ°å¤ãã渡ããã¨ãã§ããã"hoge"ãªãã¤ã¼æååã渡ããMainãæªã§ããã
- ãã°ã¯ãªã説
- ãã®ããã°ã©ã ãå®è¡ããã¨NumberFormatExceptionãçºçãããã¨ããã®ãæ£å¸¸ãªåä½ã§ããã
- ä»®ã«NFEãçºçããªããã°ãã©ããã«ãã°ãããã
- ãã®ä»ãã¾ã ããããï¼
ä¸è¨è«¸èª¬ã®ä¸ãããæ確ã«ããç°å¢ã«ä¾åããããã©ã¤ãã¦ã§ã¤ãã«ãå¯ä¸ãé¸ã³åºããã¸ãã¯ããããåèªä½ã£ã¦ã¿ã¦æ¬²ãããç§ãæ¡ç¨ãã¦ãããã¸ãã¯ã¯å¾æ¥ãç´¹ä»ããã
*1:ãªãã¦ã±ã¼ã¹ãããã®ãç¥ãããã©
*2:ä¸äººããã¸ã§ã¯ãã ã£ãã¨ãã¦ããæã®èªåãã¨ãä»ã®èªåãã®éã®æ´åæ§ãåãã®ã«æå¹ã ã¨æãã
*3:ãã ããè¨èã§è¡¨ç¾ããã®ã§ããç¨åº¦ææ§ã«ãªããã¨ã¯é¿ããããããã¹ãã¨ãã©ã¼ãã§å¯¾å¿ãã¹ããã¨ããæããã
*4:ã¢ããªã±ã¼ã·ã§ã³ã¬ãã«ã§ã®ãã°ã§ã¯ãªããã³ã¼ãã®ç²åº¦ã«ããããã°ã®ãã¨