ã柴田 芳樹 (Yoshiki Shibata):So-netブログã«åºã¦ããããã¡ã½ãããé¢æ°ã®æå¾ã§ããreturnæãæ¸ãã¦ã¯ãããªãã¨ãããã¸ã§ã¯ãã®æ¨æºã¨ãã¦ä½ããã®æ¹æ³ã§å¼·å¶ããããå ´åãæ©æ¢°çãªæ¸ãæããè¡ããã¨ã§å¯¾å¿ãããã¨ãåºæ¥ãã
ãã¾ãã¯æ»ãå¤ãvoidã®ã¡ã½ãããæ¡ä»¶åå²ãªã©ã§returnãã¦ãããããªã±ã¼ã¹ã
public void hoge1a(boolean b) { if (b) { System.out.println("true"); return; } else { System.out.println("false"); return; } }
ãããã¯ã©ãã«ä»ããããã¯+breakæã«ç½®ãæãããã¨ã§åç´ã«ç½®æã§ããã
public void hoge1b(boolean b) { ret:{ if (b) { System.out.println("true"); break ret; } else { System.out.println("true"); break ret; } } }
ã次ã«æ»ãå¤ãè¿ãå ´åãå¤æ°å®£è¨ãå ãããåºæ¬çã«ã¯ä¸ç·ã
public String hoge2a(boolean b) { if (b) { return "true"; } else { return "false"; } }
public String hoge2b(boolean b) { String ret; ret:{ if (b) { ret = "true"; break ret; } else { ret = "false"; break ret; } } return ret; }
ãswitchæãªã©ã使ã£ã¦ããå ´åã§ãåãããã«å¯¾å¿ãããã¨ãåºæ¥ãã
public String hoge3a(int i) { switch (i) { case 1: return "1"; case 2: return "1"; case 3: return "1"; default: return "other"; } }
public String hoge3b(int i) { String ret; ret:{ switch (i) { case 1: ret = "1"; break ret; case 2: ret = "1"; break ret; case 3: ret = "1"; break ret; default: ret = "other"; break ret; } } return ret; }
ç®çã®å¤±ãããã«ã¼ã«
ãã«ã¼ã«ã¨ããã®ã¯æ¬æ¥ã¯ç®çããã£ã¦å¶å®ããããã¡ã½ããéä¸ã§ã®returnãç¦ãããã¨ãã人ã¯ãã©ãããçç±ã§ãä½ãçã£ã¦ãã®ãããªã«ã¼ã«ãå®ããã®ãã
ãã«ã¼ã«ã ãããåé¢éãå®ãã®ã§ããã°ãåç´ãªæ¸ãæãã§å¯¾å¦ãããã¨ãã§ããããã ããä¸è¨ã®æ¸ãæãã¯ãã¡ã½ããéä¸ã§ã®returnç¦æ¢ãã®æå³ã«ã¯ããããªãã®ã§ã¯ãªãã ãããã
ãã¾ãããã®æ段(ã«ã¼ã«)ãç®çãéæããããã®æ¹æ³ã¨ãã¦æ©è½ããªãå ´åããã®ã«ã¼ã«ã¯è¨æ£ãããã¹ãã ãç®çã失ããã¦ãã¾ã£ãå ´åãè¨æ£ãéæ ã«å°é£ã«ãªãã
ãã¨ãã«ãå¯èªæ§ããªã©ã¨ãã主観ã«æ ¹ãããè©ä¾¡è»¸ãçç±ã¨ããã³ã¼ãã£ã³ã°ã«ã¼ã«å¶å®ã¯ããã®ç®çã失ããªãããã«æ³¨æããªãã¦ã¯ãããªãã