å¿ è¦ãªç°å¢
ãä»åå¿ è¦ãªç°å¢ã¯JDKã¨Eclipseã«ãªãã¾ããä»å使ã£ã¦ãããã®ã¯ä¸è¨ã«ãªãã¾ããéçºç°å¢ã®æ§ç¯æé ã¯é£è¼ã®ç¬¬1åãåç §ãã¦ãã ããã
- JDK8
- Eclipse Kepler
ãã¾ãããã®é£è¼ã§ã¯Eclipseã®ã³ã³ãã¤ã©æºæ ã¬ãã«ã¯1.8ï¼JDK8ã§ã³ã³ãã¤ã«å¯è½ãªã¬ãã«ï¼ã«ãã¦ãã¾ãã
1. Javaã®ä¸»ãªæ¼ç®ã¨å¶å¾¡
ãJavaã§ã¯ãã¡ã½ããã®å¦çå 容ãè¨è¿°ããã®ã«è¨ç®ãæ¡ä»¶åå²ãªã©ã®ãã¾ãã¾ãªæ¼ç®ãå¶å¾¡ãè¡ãã¾ããããã¦ããããã®å¦çã®çµæãå¼ã³åºãå ã«è¿ãããããªãããã®å½¢ã§åºåããããªã©ãã¾ããä»åã¯ãã®æ¼ç®ãå¶å¾¡ã«ã¤ãã¦ã次ã®é çªã§è¦ã¦ããã¾ãã
- ç®è¡æ¼ç®
- æ¯è¼æ¼ç®
- è«çæ¼ç®
- å¶å¾¡æ§æ
2. ç®è¡æ¼ç®
ãJavaã§ã¯ã足ãããå¼ãããæããããå²ããã®ååè¨ç®ã¨ã¨ãã«ãå²ãç®ã®ä½ããç®åºããè¨å·ï¼æ¼ç®åï¼ãã¯ã©ã¹ã®ã¡ã½ããã§ç®è¡æ¼ç®ãè¡ããã¨ãã§ãã¾ããããã§ã¯ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãä½æããªããJavaã®åºæ¬çãªç®è¡æ¼ç®ã«ã¤ãã¦è¦ã¦ããã¾ãã
ãã¾ããç®è¡æ¼ç®ãè¡ãéã«ã½ã¼ã¹ã³ã¼ãã«æ°å¤ãè¨è¿°ããã®ã§ããããã®éã«ä¸è¬çãªæ°å¤ï¼10é²æ°ã®æ°å¤ï¼ã§è¨è¿°ããå ´åãJavaã§ã¯å°æ°ç¹ãå«ã¾ããªãæ°å¤ãintã«ãå°æ°ç¹ãå«ã¾ããæ°å¤ãdoubleã¨ãã¦æ±ãã¾ãã
ãããã§ã¯Javaã®ç®è¡æ¼ç®ãè¦ã¦ã¿ã¾ãããã
2.1. 主ãªç®è¡æ¼ç®åã®ç¨®é¡
ãJavaã§ã¯ç®è¡æ¼ç®åã¨ãã¦ä¸»ã«æ¬¡ã®ãã®ã使ããã¾ãã
æ¼ç®å | æ¦è¦ |
+ | 足ãç®ï¼å ç®ï¼ |
- | å¼ãç®ï¼æ¸ç®ï¼ |
* | æãç®ï¼ä¹ç®ï¼ |
/ | å²ãç®ï¼é¤ç®ï¼ |
% | å²ãç®ã®ä½ãï¼å°ä½ç®ï¼ |
ãJavaã§ã¯ãããã使ã£ã¦æ°å¤ã®è¨ç®ãè¡ãã¾ããããã¦ãè¨ç®ã®ä»æ¹ããå·¦ããé ã«è¨ç®ããã®ã§ã¯ãªããæ¼ç®åã®ç¨®é¡ãã()ãï¼ä¸¸ã«ãã³ï¼ã®ä½¿ç¨ãªã©ã§åªå é ä½ãé«ããã®ããè¨ç®ããããã«ãªã£ã¦ãã¾ãã
ãæ¼ç®åã®åªå é ä½ã¯ä¸è¬çãªæ°å¤è¨ç®ã®åªå é ä½ã¨åãããã()ãå ã®è¨ç®ãæåªå ãããã*ãï¼æãç®ï¼ãã/ãï¼å²ãç®ï¼ãã%ãï¼å²ãç®ã®ä½ãï¼ã®ã»ããã+ãï¼è¶³ãç®ï¼ãã-ãï¼å¼ãç®ï¼ããå ã«è¨ç®ããã¾ãã
ãããã§ã¯æ¬¡ã®ã½ã¼ã¹ã³ã¼ããå®è¡ãã¦ç®è¡æ¼ç®åã«ã¤ãã¦è¦ã¦ã¿ã¾ãããã
package jp.codezine.java.sample03; /** * ç®è¡æ¼ç®åã確èªããã¯ã©ã¹ã */ public class ArithmeticOperator1 { /** * ç®è¡æ¼ç®ãå®è¡ãããã®å 容ã確èªãã¾ãã * * @param args * ã³ãã³ãã©ã¤ã³å¼æ°ãä»åã¯ä½¿ããã¾ããã */ public static void main(String[] args) { // [1] 足ãç® int result = 1 + 1; System.out.println("[1] 1 + 1 = " + result); // [2] å¼ãç® result = 10 - 1; System.out.println("[2] 10 - 1 = " + result); // [3] æãç® result = 3 * 2; System.out.println("[3] 3 * 2 = " + result); // [4] å²ãç® result = 7 / 3; System.out.println("[4] 7 / 3 = " + result); // [5] ä½ãï¼7÷3ã®ä½ãï¼ result = 7 % 3; System.out.println("[5] 7 % 3 = " + result); // [6] åªå é ä½ããã result = 2 + 3 * 4; System.out.println("[6] 2 + 3 * 4 = " + result); // [7] ã«ãã³å ã®ãã®ãåªå ãã¦è¨ç®ããã result = 4 * (2 + 3); System.out.println("[7] (2 + 3) * 4 = " + result); } }
ãå®è¡ããã¨ä¸è¨ã®çµæã表示ããã¾ãã
[1] 1 + 1 = 2 [2] 10 - 1 = 9 [3] 3 * 2 = 6 [4] 7 / 3 = 2 [5] 7 % 3 = 1 [6] 2 + 3 * 4 = 14 [7] (2 + 3) * 4 = 20
2.2. ç¹æ®ãªç®è¡æ¼ç®åã®ä½¿ãæ¹
ãJavaã§ã¯ä¸è¬çãªç®è¡æ¼ç®ã®è¨è¿°ã®ã»ãã«ãããã°ã©ãã³ã°è¨èªç¹æã®è¨è¿°æ¹æ³ãããã¾ãã主ãªãã®ã«æ¬¡ã®æ¸å¼ã®ãã®ãããã¾ãã
å¤æ° += å¤æ°; // â» æ¼ç®åã¯ã+ãã®ã»ãã«ã-ããã*ããã/ããã%ãã使ãã¾ã ++å¤æ°; // â» æ¼ç®åã¯ã++ãã®ã»ãã«ã--ãã使ãã¾ã å¤æ°++; // â» æ¼ç®åã¯ã++ãã®ã»ãã«ã--ãã使ãã¾ã
ãä¾ãã°ãintã®å¤æ°ãresultããããããresult += 2ãã¯ãresult = result + 2ãã表ãã¦ãã¦ãããã¯è¨ç®åã®å¤ã1ã®å ´åããå¤æ° = 1 + 2ãã¨åãã«ãªãã¾ãããã®è¨è¿°ã¯è¶³ãç®ã®ã+ãã ãã§ã¯ãªãä»ã®ç®è¡æ¼ç®åã®ã-ãã*ãã/ãã%ãã§ãè¡ããã¨ãã§ãã¾ãã
ãã¾ããã++ãã¯å¤æ°ã«å¯¾ã1ãå ç®ãããã¨ã表ãã¦ãã¾ãããã ã注æç¹ã¨ãã¦ããã®ã++ããåã«æ¸ããå¾ãã«æ¸ããã§ããã®å¤æ°ãè¨ç®ãããã¿ã¤ãã³ã°ãå¤ããã¾ããä¾ãã°ãintã®å¤æ°ãresultãããããã++resultãã®ããã«åã«æ¸ãããã®ãå¼æ°ã¨ãã¦ã¡ã½ããã«æ¸¡ããå ´åãresultã¯1ãå ç®ãããç¶æ ã§æ¸¡ããã¾ããéã«ãresult++ãã¨å¾ãã«æ¸ããå ´åãresultã¯1ãå ç®ãããåã®ç¶æ ã§æ¸¡ããã¾ããããã¦å¼æ°ã¨ãã¦æ¸¡ãããå¾ã«1ãå ç®ããã¾ãããã®è¨è¿°ã¯ã++ãã ãã§ãªã1ãæ¸ç®ããã--ãã§ãè¡ããã¨ãå¯è½ã§ãã
ãããã§ã¯æ¬¡ã®ãµã³ãã«ãå®è¡ãã¦ç®è¡æ¼ç®åã«ã¤ãã¦è¦ã¦ã¿ã¾ãããã
package jp.codezine.java.sample03; /** * ç®è¡æ¼ç®åã確èªããã¯ã©ã¹ã */ public class ArithmeticOperator2 { /** * ç®è¡æ¼ç®ãå®è¡ãããã®å 容ã確èªãã¾ãã * * @param args * ã³ãã³ãã©ã¤ã³å¼æ°ãä»åã¯ä½¿ããã¾ããã */ public static void main(String[] args) { // [1] ç¹æ®ãªè¨ç® int result = 1; result += 2; System.out.println("[1] result = 1 â result += 2 â result = " + result); // [2] ç¹æ®ãªè¨ç® System.out.println("[2] result = 1 â ++result"); result = 1; printValue(++result); System.out.println("result = " + result); // [3] ç¹æ®ãªè¨ç® System.out.println("[3] result = 1 â result++"); result = 1; printValue(result++); System.out.println("result = " + result); } /** * åãåã£ãvalueãæ¨æºåºåãã¾ãã * * @param value * å¤ */ private static void printValue(int value) { System.out.println("åãåã£ãå¤ï¼ value = " + value); } }
ãããã§ã¯ã++ãæ¼ç®åã«ããè¨ç®ãè¡ãããã¿ã¤ãã³ã°ã確èªãããããprintValueã¡ã½ãããä½æãã¦ãå¼æ°ã«ã++ãæ¼ç®åã使ã£ããã®ã渡ãã¦ãã¾ããããã¦ããã®åãåã£ãå¤ãprintValueã¡ã½ããã§è¡¨ç¤ºãã¦ãã¾ãããããå®è¡ããã¨ã次ã®çµæãåºåããã¾ãã
[1] result = 1 â result += 2 â result = 3 [2] result = 1 â ++result åãåã£ãå¤ï¼ value = 2 result = 2 [3] result = 1 â result++ åãåã£ãå¤ï¼ value = 1 result = 2
ããã®çµæãã[2]ã®++resultã¯æ¼ç®åã§è¨ç®ãããå¤ã渡ããã[3]ã®result++ã¯æ¼ç®åã§è¨ç®ããåã®å¤ã渡ããã¦ããã®ãåããã¾ãã