æ¥åç³»ã®Javaããã°ã©ãã¼ãç¥ã£ã¦ããã¹ã10åã®Bad Partsã¨ãã®å¯¾ç
Java: The Good Partsã®æ¬ã®ã¿ã¤ãã«ã«è§¦çºããã¦ãéã«Javaè¨èªã®ä½¿ãã«ããé¨åãããã¤ãããã¯ã¢ãããã¦ã¿ã¾ãããJava EEãªã©ã®æ¥åç³»ã®ã¢ããªã±ã¼ã·ã§ã³ããã°ã©ãã¼ã®è¦ç¹ã§æ¸ãã¦ãã¾ãã®ã§ãå¥ã®ç«å ´ã§ã¯ããã§ææãã¦ããäºé ãå¿ ãããBad Partã§ã¯ãªãã¨ããææãããããããã¾ããããä»ã«ããããããªãã¤ã³ããããã¨æãã¾ãããã¨ãããããç§ã®ç¬æã§æãã¤ãããã®ã10å説æãããã¨æãã¾ãã
1.æ¨æºAPIã®ãã§ãã¯ä¾å¤ãæ±ãã«ãã
Java言語のチェック例外は本当にGood Partなのか? - 達人プログラマーを目指してã§ãåãä¸ãã¾ããããBad Partã®ç¬¬ä¸çªç®ã¨ãã¦æ¨æºAPIã®ãã§ãã¯ä¾å¤ãæ±ãã«ããã¨ããç¹ãææããã¦ããã ãããã¨æãã¾ãããã§ãã¯ä¾å¤ã«ã¤ãã¦ã¯ãçå±ä¸ã³ã³ãã¤ã©ã¼ã«ãã£ã¦ä¾å¤ã®å¦çãããã°ã©ãã¼ã«å¼·å¶ããããã¨ãã§ãããã°ãããèãæ¹ã®ããã«æããã¾ãããå®éã«ä»¥åã¯ãã®ããã«ä¿¡ãããã¦ããã¨ãããããã¾ãããå°ãªãã¨ãæ¥åã¢ããªã±ã¼ã·ã§ã³éçºã®é åã«ããã¦ãJava APIã®ãã§ãã¯ä¾å¤ãéªéã«ãªã£ããã¨ã¯ããã便å©ã ã¨æãããã¨ã¯ã»ã¨ãã©ããã¾ãããç¹ã«ã
- ãªãã¬ã¯ã·ã§ã³APIã使ãå ´å
- RMI APIãç´æ¥ä½¿ãå ´å
- JDBC APIãç´æ¥ä½¿ãå ´å
- JAXPãªã©ã®XMLé¢é£ã®APIãç´æ¥ä½¿ãå ´å*1
- J2EEç³»ã®APIï¼JNDIãEJB2.xãJMSãJava Mailãªã©ï¼ãç´æ¥ä½¿ãå ´å
ãªã©ã®å ´åãä¾å¤ã¯ã¢ããªã±ã¼ã·ã§ã³å´ã§å¯¾å¦å¯è½ãªåå ã§çºçããªããã¨ãã»ã¨ãã©ã§ãé¢åãªãã¨ã¯ãã£ã¦ãå½¹ã«ç«ã£ã¦ããã¨ããå®æã¯æãããã¾ãããããã¨ãã°ããªãã¬ã¯ã·ã§ã³ã使ã£ã¦ç¹å®ã®ãã£ã¼ã«ãã®å¤ãèªã¿è¾¼ã以ä¸ã®ã³ã¼ããèãã¦ã¿ã¦ãã ããã
Field field; try { field = getClass().getField("testField"); Object value = field.get(this); } catch (SecurityException e) { // ä¾å¤å¦ç } catch (NoSuchFieldException e) { // ä¾å¤å¦ç } catch (IllegalAccessException e) { // ä¾å¤å¦ç }
åã«åçã«ãã£ã¼ã«ãã®å¤ãåå¾ãããã ããªã®ã«ãç¸å½é¢åãªã³ã¼ãã®è¨è¿°ãå¿ è¦ãªã®ã§ãæ¬æ¥ä¾¿å©ãªãªãã¬ã¯ã·ã§ã³APIã®æ·å± ãå¿ è¦ä»¥ä¸ã«é«ããªã£ã¦ãã¾ã£ã¦ãã¾ãã*2ãããJDBCãªã©ã¨çµã¿åãããã¨ããã«è¤éãªãã¨ã«ãªãã®ã¯å®¹æã«æ³åã§ããã§ããããå®éã«ä¸è¨ã®ä¾å¤ã«å¯¾ãã¦ã¢ããªå´ã§å¯¾å¦ã§ãããã¨ã¯ãããã
- ãã°ãè¨é²ãã
- å®è¡æä¾å¤ã«å¤æãã¦åéåºãã
- ä¾å¤ãæ»ãå¤ã«å¤æãã
- VMãã·ã£ãããã¦ã³ãã
ã¨ãã£ã決ã¾ããã£ããã¨ããã§ããªãã®ãæ®éã§ããããããã£ãä¾å¤å¦çã¯AOPã§èªååãã¦ãã¾ãããã®ã§ããæ¨æºAPIã®ãã§ãã¯ä¾å¤ã§ãç§ãæã 便å©ã¨æããããã®ã¯
- ParseExceptionãªã©å ¥åãã§ãã¯ã®ä¾å¤
- IOExceptionãªã©IOç³»ã®ä¾å¤
- InterruptedExceptionãªã©ã¹ã¬ããã®å²ãè¾¼ã¿æã®ä¾å¤
ãããã§ããããã
ç§ã¯ãã§ãã¯ä¾å¤ã®åå¨æ義ã«ã¤ãã¦æ£ããç解ããã«ã¯ããã¨ãã¨Javaããã¼ã¸ã§ã³1ã§ç»å ´ãã¦ããæ代ã®ãã¨ãèããã¹ãã ã¨æãã¾ããä»ã§ã¯ä¿¡ããããªãããããã¾ãããããã®æ代ã®Javaã«ã¯ãµã¼ãã¬ãããJDBCããªãã¬ã¯ã·ã§ã³APIãããã¾ããã§ãããä»ã§ã¯å¸¸èã®JavaBeansã¨ããæ¦å¿µãããªãã£ãã®ã§ãããã®é ã¯ã¢ãã¬ããã¨ããè¨èã«ä»£è¡¨ãããããã«æ°åã®ã¯ã©ã¹ãããªãå°è¦æ¨¡ãªã¢ããªã±ã¼ã·ã§ã³ããããã¯ã¼ã¯éä¿¡ã®ã¦ã¼ãã£ãªãã£ãªã©ãä½æããè¨èªã§ããã¨èãããã¦ãã¾ãããã¤ã¾ããå°è¦æ¨¡ã§ãã«ãã¹ã¬ãããªããã°ã©ã ãä½æããã®ã«ç¹åãã¦ããã¨ããã¨ãããããã¨æãã¾ãããã®ãããªã³ã³ããã¹ãã§IOExceptionãInterruptedExceptionãªã©ããã§ãã¯ä¾å¤ã¨ãã¦å®ç¾©ããã¾ãããæ®å¿µãªãããã®æã®æåã«ãã£ã¦ãããããå¾ã«ç»å ´ããAPIã§ã¯ç²ç®çã«ãã§ãã¯ä¾å¤ã使ãã¹ãã¨ããåºæºãã§ãã¦ãã¾ã£ãã®ã ã¨æ¨æ¸¬ããã¾ããããã¦ãå
¸åçãªGolden Hammerã¢ã³ããã¿ã¼ã³*3ã«é¥ã£ã¦ãã¾ãã以éãæ¬æ¥ä¸é©åãªé¨åã«ã¾ã§ãã§ãã¯ä¾å¤ãå©ç¨ããã¦ãã¾ã£ãã®ã§ã¯ãªããã¨æãããã®ã§ãã
ãã§ãã¯ä¾å¤ã«å¯¾ãã¦ã¯ãシステム系の例外は実行時例外+AOPでハンドリングするのがベスト - 達人プログラマーを目指してã§èª¬æããããã«Springãªã©ã®ãã¬ã¼ã ã¯ã¼ã¯ã使ã£ãããAOPã使ã£ãããããã¨ã§ãé©åãªéãã§ãã¯ä¾å¤ã«å¤æãã¦å¦çããã®ãç¾å®çã ã¨æãã¾ããã¾ããScalaãGroovyãªã©ã®è»½éè¨èªãå©ç¨ããã®ãããã¨æãã¾ããç¾ä»£çãªã¢ã¼ããã¯ãã§ããã°ãå®éã«æ¥åãã¸ãã¯ã®éçºãè¡ãããã°ã©ãã¼ãè¦ãã¾ãªãããã«ãé©åãªããã°ã©ãã³ã°ã¢ãã«ãæ¨æºåãã¦ãããã¨ã大åã ã¨æãã¾ãã*4
2.ãªã½ã¼ã¹ã®è§£æ¾å¦çã®é©åãªè¨è¿°ãé¢å
ä¸è¨ã®ãã§ãã¯ä¾å¤ã®åé¡ã¨ãé¢é£ãã¦ããã®ã§ãããJavaã§ä¾å¤çºçæã«æ£ãããªã½ã¼ã¹ã®è§£æ¾å¦çãè¨è¿°ãããã¨ã¯é¢åã ããå®éã«åå¿è
ã®äººãééãããããã¤ã³ãã¨ãªããã¡ã§ãã
ãã¨ãã°ããã¡ã¤ã«å
¥åºåå¦çãJavaã®æ¨æºAPIã®ã¿ã使ã£ã¦è¨è¿°ããã¨ä»¥ä¸ã®ããã«è¨è¿°ããå¿
è¦ãããã¾ãã
// tryãããã¯ã®å¤ã§å®£è¨ Reader in = null; try { in = new BufferedReader(new FileReader("ãã¡ã¤ã«å")); // å ¥åå¦ç } catch (IOException e) { // ä¾å¤å¦ç } finally { // finallyãããã¯ã§å¾å¦ç if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
以ä¸ã®ãããªæ¸ãæ¹ã«ã¤ãã¦ã¯ãã¤ãã£ãªã ã¨ãã¦è¨è¿°æ¹æ³ã丸æè¨ãã¦ãããã¨ãèè¦ã§ããããã®ãããªè¤éãªè¨è¿°ã¯åå¿è
ã®äººã«ã¨ã£ã¦ã¯é£ããã¨æãã¾ããã*5
ç¾å¨ã§ã¯ãIOé¢é£ã®APIã¯ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ä¸ã§ç´æ¥å©ç¨ããã®ã§ã¯ãªããæä½ã§ãCommons IOãªã©ã®ã©ã¤ãã©ãªã¼ãå©ç¨ãããã¨ãèããã¹ãã§ããã¾ãã決ã¾ããã£ãå¦çã§ããã°Template Methodãã¿ã¼ã³ã使ã£ããã¬ã¼ã ã¯ã¼ã¯ãç¬èªã«å·¥å¤«ãã¦ãæ¥åãã¸ãã¯ã®è¨è¿°è
ãæ¬è³ªçãªãã¸ãã¯ã®ã¿ã«éä¸ã§ããããã«å·¥å¤«ããã¹ãã§ããããSpring Frameworkã®JdbcTemplateãªã©ã®è¨è¨ãåèã«ãªãã¾ãã
ãããããç¾ç¶ãJavaããã°ã©ãã¼ãã¨ããã対å¦ã¨ãã¦ãã£ã¨è¯ãã®ã¯Groovyãä½µç¨ãããã¨ã§ããããã¨ãã°ããã¡ã¤ã«ã®èªã¿è¾¼ã¿å¦çã§ããã°ã親ãã¿ãããJavaã©ã¤ã¯ãªæ§æã使ããªããã以ä¸ã®ããã«Rubyãªã¿ã«ç°¡åã«è¨è¿°ã§ããããã«ãªãã¾ãã
new File('ãã¡ã¤ã«å').eachLine {line -> // line ã«åè¡ã®æååãå ¥ã£ã¦ããã } // ãªã½ã¼ã¹ã®è§£æ¾ã¯èªåçã«è¡ãããããèããªãã¦è¯ãã
ï¼è¿½è¨ï¼ã³ã¡ã³ãæ¬ã«ã¦ãææããã£ãã®ã§ãããä»å¹´ã®å¤ãªãªã¼ã¹ãäºå®ããã¦ããJava SE7ã§ã¯ãªã½ã¼ã¹ã®èªå解æ¾æ©è½ã®ããã®æ§ææ¡å¼µãè¡ãããããã§ãããã¨ãã¨ã®Javaã®ä¾ã ã¨ä»¥ä¸ã®ããã«æ¸ããã¿ããã§ãã
try (Reader in = new BufferedReader(new FileReader("ãã¡ã¤ã«å"))) { // å ¥åå¦ç } catch (IOException e) { // ä¾å¤å¦ç } // ãªã½ã¼ã¹ã¯èªåçã«è§£æ¾ãããã
å°å³ãªæ¡å¼µã§ãããç¾ç¶ã¨æ¯ã¹ãã¨ããªã便å©ããããã¾ããã
Redirecting...
3.æ¥ä»é¢é£APIã®ä½¿ãåæãæªã
æ¥åã¢ããªã±ã¼ã·ã§ã³ã§ã¯æ¥ä»åã®ãã¼ã¿ãæ±ããã¨ã¯ããªãé »ç¹ã«ããã¾ãããJavaã®æ¥ä»é¢é£ã®APIã¯ãã¾ã使ãåæã®è¯ããã®ã§ã¯ããã¾ãããç¹å®ã®æ¥æã®æ¥ä»ãçæãããããã©ã¼ããããããããã®ã«ããªãé¢åãªã³ã¼ãã£ã³ã°ãå¿
è¦ã«ãªãã¾ããã¾ããJUnitã§æ¥ä»é¢é£ã®ãã¹ããä½æããã®ã¯ã¦ã¼ãã£ãªãã£ã工夫ããªãã¨ãããªãé¢åã«ãªã£ã¦ãã¾ãã¾ãã
ãã¨ãã°ããã®æç« ãæ¸ãã¦ããä»æ¥ã®æ¥ä»ãçæããã«ã¯ä»¥ä¸ã®ãããªã³ã¼ããå¿
è¦ã§ãã
// ä»æ¥ã®æ¥ä»ãçæ Calendar cal = Calendar.getInstance(); cal.set(2011, Calendar.FEBRUARY, 26); Date today = cal.getTime(); // yyyy/mm/ddã§ãã©ã¼ããã DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); df.format(today);
ããã«ãæ¥ä»ã®è¨ç®ãDateã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ããæ®éã®æ¼ç®åãã¡ã½ããå¼ã³åºãã§ãªããCalendarã使ãå¿
è¦ãããçµæ§é¢åã§ããããã¦ãã®ã¾ã¨ããªããã¸ã§ã¯ãã§ããã°ãããããå¦çãç°¡æåããã¦ã¼ãã£ãªãã£ã¯ã©ã¹ãä½æãã¦å¯¾å¦ãã¦ããã¨æãã¾ãããJoda Timeã®ãããªã©ã¤ãã©ãªã¼ãå©ç¨ããã®ãããã¨æãã¾ãã
Joda-Time
ã¾ããGroovyã ã¨ä»¥ä¸ã®ããã«ç°¡åã«æååã¨ç¸äºå¤æã§ããããè¨ç®ã§ããããã¾ãã
Date day = Date.parse('yyyy/MM/dd', '2011/2/26') Date nextDay = day + 1 println nextDay.format('yyyy/MM/dd')
4.ç¹å®ã®ç²¾åº¦ãä¿ã£ãéé¡è¨ç®ãé¢å
æ¥ä»ã¨åæ§ã«ãç¹å®ã®ç²¾åº¦ã§æ¡è½ã¡ããªã¼ãã¼ããã¼ããªãæ¼ç®ããéãæ±ããããªæ¥åã·ã¹ãã ã§ã¯ããå¿
è¦ã«ãªãã¾ããJavaã®å ´åã¯java.mathããã±ã¼ã¸ã®BigDecimalã¨ããã¯ã©ã¹ã使ã£ã¦ãã¼ã¿ã®ä¿æã¨è¨ç®ãè¡ããjava.text.DecimalFormatãç¨ãã¦æååã«ãã©ã¼ããããã¾ããããããæ¥ä»ã¨ä¸¦ãã§é¢åãªã¨ããã§ãããã¨ãã¨Javaã¯æ¥åç³»ã¨ããããã¯Cã®ãããªã·ã¹ãã ç³»ã®ããã°ã©ãã³ã°ãæ³å®ãã¦ããã®ããããã¾ããããè¨èªã¨ãã¦ãããã£ãéé¡è¨ç®ã®ãµãã¼ãããªããã©ã¤ãã©ãªã¼ã¨ãã¦å¾ä»ãã§å¯¾å¿ãã¦ãããããä¸ä¾¿ã«ãªã£ã¦ããã®ã ã¨æãã¾ãã
ã¾ããJavaãC#ã®ããã«èªåçã«å¤ãã³ãã¼ãããå¤åã®ãªãã¸ã§ã¯ããæããªããã¨ããä»æ¹ãç¡ãã®ã§ãããBigDecimalã¯ä¸å¤ã¯ã©ã¹ã¨ãã¦è¨è¨ããã¦ãã¦ãè¨ç®çµæã¯å¥ã®ã¤ã³ã¹ã¿ã³ã¹ã«ãªããããå¿ããã«è¨ç®çµæãå代å
¥ããå¿
è¦ãããã¾ããæ®éã®ååæ¼ç®ã®æ¼ç®åã使ãããªãåé¡ãªãã®ã§ãããã¡ã½ããå¼ã³åºãã®å½¢å¼ã«ãªã£ã¦ããã¨ãããã§ãã£ããã¿ã¼ã²ããã®ãªãã¸ã§ã¯ãã®ç¶æ
ãå¤æ´ããã¦ããã¨åéããã¦ãã¾ããã°ãããã¾ããï¼ãã®ãã°ã¯FindBugsã§æ¤åºã§ãããï¼
BigDecimal num1 = new BigDecimal("123456.789"); BigDecimal num2 = new BigDecimal("111.111"); // num1ã®ç¶æ ã¯ä¸å¤ãªã®ã§çµæãå¥ã®å¤æ°ã§åãåã BigDecimal result = num1.multiply(num2); // æ®éã®æ¼ç®åã使ããªãã®ã§è¤éãªè¨ç®ãé¢åã
Groovyã ã¨æ°å¤ãªãã©ã«ã®æå¾ã«Gã¨ããæ¥å°¾è¾ãã¤ããã ãã§èªåçã«BigIntegerãBigDecimalãçæã§ããå¾ã¯æ®éã«ååæ¼ç®ã®è¨å·ã使ã£ã¦è¨ç®ã§ãã¾ãã
def result = 123456.789G * 111.111G // èªåçã«BigDecimalã使ã£ã¦è¨ç®ããã
5.æ¨æºã®æååæ¼ç®APIãéããã¦ãã
Javaã®æ¨æºAPIã®ç¯å²å
ã§ã¯Stringã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦æä½å¯è½ãªã¡ã½ããã¯é常ã«éããã¦ãã¾ããã¡ãã£ã¨è¤éãªå¦çããããã¨ããã¨StringTokenizerãªã©ã使ã£ã¦ç¬èªã«ãã¸ãã¯ãä½æããå¿
è¦ãåºã¦ãã¾ããããããå ´åãç¬èªã«ãã¸ãã¯ãä½æããã®ã§ã¯ãªããå°ãªãã¨ãCommons Langã«å«ã¾ãã¦ããStringUtilsãä½µç¨ãããã¨ãèããã¹ãã§ãããããã®ã©ã¤ãã©ãªã¼ã使ãã°ã
æååã空æåãnullããå¤å¥ããã®ã«
StringUtils.isEmpty(str);
ã¨ãã£ãè¨è¿°ãã§ãã¾ãããã«ã³ãåºåãã®æååãçæããã®ã«
String[] strArray = {"data1", "data2", "data3"}; StringUtils.join(strArray);
ã®ãããªè¨è¿°ãå¯è½ã«ãªãã¾ããStringUtilsã®ãããªã¦ã¼ãã£ãªãã£ã¼ã¯ã©ã¹ã®å¤é¨ã¡ã½ããã使ã£ãé£ç¹ã¯ãè¨è¿°ãä¸èªç¶ãªãã¨ã§ãããGroovyã§ã¯Stringã¯ã©ã¹ã«ä»¥ä¸ã®ãããªã¡ã½ããã追å ãããã¨ã§ãããèªç¶ã§ä¾¿å©ã«æä½ã§ããããã«ãªã£ã¦ãã¾ãã
http://groovy.codehaus.org/groovy-jdk/java/lang/String.html
6.æ£è¦è¡¨ç¾APIã使ãã®ãé¢å
ããã¯ä»¥åã«Javaで正規表現を扱うのは難しい - 達人プログラマーを目指してã§ãç´¹ä»ãããã¨ãããã®ã§ãããJDK1.4ãã追å ãããæ£è¦è¡¨ç¾ã®APIã¯é常ã«ä½¿ãããªããã¨ãé£ããã¨æãã¾ããããããçç±ãããã®ã ã¨æãã¾ãããæ¬æ¥ã¯æ£è¦è¡¨ç¾APIã使ãã°ç°¡åããã¤ãå¹ççã«å¦çã§ãããããªã±ã¼ã¹ã§ãã£ã¦ãç¬èªã®ã«ã¼ããå¤å®ãã¸ãã¯ãæ¸ãã¦ãã¾ã人ããã¾ããæ£è¦è¡¨ç¾ã®ä½¿ãããããGroovyã§å¤§å¹
ã«æ¹åãããã¨ããã§ããã以ä¸ã«ãã¥ã¼ããªã¢ã«ãããã¾ãã
http://groovy.codehaus.org/Japanese+Tutorial+4+-+Regular+expressions+basics
7.é·ãæååã®è¨è¿°æ¹æ³ãé¢å
SQLãJPQLã®è¨è¿°ãªã©ãæ¥åã¢ããªã±ã¼ã·ã§ã³ã§ã¯é·ãæååãªãã©ã«ãã³ã¼ãä¸ã«è¨è¿°ããå¿ è¦ã®ããã±ã¼ã¹ãçµæ§ããã¾ããé常ãJavaã®å ´åã¯çãæååã«åºåã£ã¦ãããã+ãæ¼ç®åã§çµåããã¨ããæ¹æ³ãã¨ãã¾ãã
Sting jpql = "SELECT c FROM Customer " + "WHERE c.name = :name " + "ORDER BY c.id";
ãã¡ãããSQLãªã©ã¯xmlãã¡ã¤ã«ã§ç®¡çãããªã©ã®æ¹æ³ãããã¾ãããxmlã«SQLãæ¸ãå ´åãåé·ãªã¿ã°ãCDATAã»ã¯ã·ã§ã³ã®å¿
è¦æ§ãªã©ãããã¯ããã§çµæ§é¢åãªå ´åãå¤ãã§ãã
Groovyã«ã¯ã'''ãã"""ãã¨ãã3ã¤ã®ã¯ãªã¼ãã¼ã·ã§ã³ã使ã£ã¦é·ãæååãå®ç¾©ããä»çµã¿ãããã¾ãããã®è¨æ³ã使ãã°ãè¤æ°è¡ã«ãããé·ãæååãç°¡åã«å®ç¾©ã§ãã¾ãã
def jpql = ''' SELECT c FROM Customer WHERE c.name = :name ORDER BY c.id '''
8.åºæ¬åã¨ã©ããã¼ã¯ã©ã¹ã®åå¨
Javaè¨èªã«ã¯intã¨Integerãdoubleã¨Doubleã®ããã«ãåºæ¬åã¨å¯¾å¿ããåç §åã®ã©ããã¼ã¯ã©ã¹ãåå¨ãã¦ãã¾ãã*6ç§ã¯ä»ã§ã¯ãã®ç¹ã¯å¸¸èã¨ãããæ®éã«èããããããã«ãªãã¾ãããããã¯ããåãã¦Javaãå¦ç¿ããåå¿è ã®äººã«ã¨ã£ã¦ã¯ç解ãã¦æ£ãã使ãåãããã¨ãé£ããã¨ãããã¨æãã¾ããåºæ¬çãªç解ã¨ãã¦ã¯
- NOT NULLå¶ç´ã®ãªãDBã®ã«ã©ã ã¨ãããããæã®ããã«ãnullå¤ã表ç¾ããå¿ è¦ãããå ´åã¯ã©ããã¼åã使ã
- MapãListã«æ ¼ç´ããæã«ã¯ã©ããã¼åã使ã*7
- Integer.parseInt()ãªã©ã®ããã«åºæ¬åã«ä»éããã¡ã½ãããå¼ã³åºãããã¨ãã¯ã©ããã¼åã®ã¯ã©ã¹ã¡ã½ããã使ã
- ãã以å¤ã¯åºæ¬åã使ã
ã¨ããã®ã§è¯ãã¨æãã¾ãããã®ç¹ãæ´å²ä¸ã®å¶ç´ã¨ããããJavaãäºææ§ãæ¨ã¦ãªãéããããå¾ãªãåé¡ããããã¾ãããJava5ã«ãªã£ã¦èªåçãªããã¯ã¹åãéããã¯ã¹åã«ããåºæ¬åã¨ã®ç¸äºå¤æãæä¾ãããã®ã§ã以åã¨æ¯ã¹ã¦ããªãã¾ãã«ã¯ãªã£ã¦ãã¾ãããæå³ããªãNullPointerExceptionã®åé¡*8ãªã©ããã注æãå¿
è¦ã§ãã
Groovyã ã¨ãåºæ¬çã«æ°å¤ãªãã©ã«ã¯èªåçã«ã©ããã¼ã¯ã©ã¹ã«å¤æãããããããèªç¶ã«æ±ããããã«ãªã£ã¦ãã¾ãããåºæ¬åã«ã¯æ§è½é¢ã§ãªãã¸ã§ã¯ãããé¥ãã«æå©ãªã¨ããããããç»åã®å¦çãªã©å¤§éã®æ°å¤ãã¼ã¿ãå¦çããªãã¦ã¯ãªããªããããªå ´åã«ã¯ãªãã¦ã¯ãªããªããã®ã§ããæ§è½ãæ±ããããã±ã¼ã¹ã§ã¯ç¾ç¶ã®Groovyã¯ä¸å©ã§ããã
9.é ååã¨ã³ã¬ã¯ã·ã§ã³ã¨ã®ä¸çµ±ä¸
Javaè¨èªã¯C++ã®æ§æãã¾ãã¦ä½æãããã¨è¨ããã¦ãã¾ãããæ¼ç®åãªã¼ãã¼ãã¼ãã£ã³ã°ããã³ãã¬ã¼ãæ©è½*9ãç¡ãã£ããã¨ããããé
åã¨ã³ã¬ã¯ã·ã§ã³ã©ã¤ãã©ãªã¼ã«ã¤ãã¦ã¯æ±ããã¾ã£ããä¸çµ±ä¸ã«ãªã£ã¦ãã¾ããã¾ãã誰ã§ãæ°ã¥ããã¨ã¨ãã¦ä¸¡è
ã§åæåãè¦ç´ ã¢ã¯ã»ã¹ãªã©è¦ããä¸ã®æ§æä¸ã®æ±ããã¾ã£ããç°ãªãã¾ããã¾ããé
åã¯lengthãã£ã¼ã«ãã§é·ããåãåºãã®ã«ãListã¯size()ã¡ã½ããã§é·ããåå¾ãããªã©ãã¾ãã§çµ±ä¸æãããã¾ããã
ã¾ãããããã表é¢ä¸ã®éã以å¤ã«ããGenericsãèããã¨é
åã¨ãã以å¤ã®ã³ã¬ã¯ã·ã§ã³ã«å¯¾ãã¦ã¯å¤§ããªä¸çµ±ä¸ãããã¾ããé
åã®åã¯å®éã«ã³ã³ãã¤ã«çµæã®ãã¤ãã³ã¼ãä¸ã«åæ
å ±ãåãè¾¼ã¾ãã¾ã*10ãã¤ã¾ããString[ ]ã§å®£è¨ãããå¤æ°ã¨Integer[ ]ã§å®£è¨ããããªãã¸ã§ã¯ãã¯ã¾ã£ããå¥ã®åã¨ãã¦æ±ããã¾ããä¸æ¹ã§ãList
List<T> list = new ArrayList<T>(); // OK T[] array = new T[3]; // ã³ã³ãã¤ã«ã¨ã©ã¼ãç·ç§°åã®é åãä½æã§ãã¾ããã
以ä¸ã®ããã«ãªãçç±ãç解ã§ããã§ãããããé
ååã¯åæ
å ±ããã¤ãã³ã¼ãä¸ã«åãè¾¼ã¾ãããããnewã§çæããããã«ã¯å®è¡æã«åæ
å ±ãç¥ãå¿
è¦ãããã¾ããã¨ããããå³è¾ºã®newãå®è¡ããã段éã§ã¯åTã®æ
å ±ã¯æ®ã£ã¦ãã¾ããã®ã§ãå®è¡æã«é©åãªåã®ãªãã¸ã§ã¯ããçæãããã¨ãã§ãã¾ãããã¤ã¾ããJavaã§new T()ã¨ããæ¸ãæ¹ãã§ããªãã®ã¨åãçç±ã§new T[]ã¯è¨±ããã¾ããã*12ããããList
Genericsã«ã¤ãã¦ã¯ãããä¸ã¤å¤§ããªéãã¨ãã¦åãã©ã¡ã¼ã¿ã¼ã«å¯¾ãã¦å
±å¤æ§ï¼covarianceï¼ã®ããé
åã¨éãã³ã¬ã¯ã·ã§ã³ã®åã¯éå¤æ§ï¼invarianceï¼ãããã¨ããç¹ã§ããã*13ã¤ã¾ãã
String[] strArray = ...; Object[] objArray = ...; List<String> strList = ...; List<Object> objList = ...; objArray = strArray; // OK objList = strList; // ã³ã³ãã¤ã«ã¨ã©ã¼ãList<String> ãã List<Object> ã«ã¯å¤æã§ãã¾ããã
ã¨ãããã¨ã§ããä¸è¬ã«è¦ç´ ã®è¿½å ãå¯è½ãªã³ã¬ã¯ã·ã§ã³ã«å¯¾ãã¦åå®å
¨æ§ãèããã¨å¦¥å½æ§ã®ããä»æ§ãªã®ã§ãããæã
ã³ã³ãã¤ã«ã¨ã©ã¼ã§è¦å´ããç®æã§ããã*14
ãã®ç¹ãå³å¯ãªåå®å
¨æ§ãç ç²ã«ããå¿
è¦ãããã¾ãããGroovyã¯é常ã«ããé¸æè¢ã«ãªãã¾ããåºæ¬çã«Listãé
åã®åæåãè¦ç´ ã¢ã¯ã»ã¹ã¯ä»¥ä¸ã®ããã«çµ±ä¸ãã¦è¨è¿°ã§ãã¾ãã
// æ®éã¯é åã¨Listã®éããæèããå¿ è¦ã¯ãªãã String[] strArray = ['test', 'test2'] List<String> strList = ['test', 'test2'] // ã¤ã³ããã¯ã¹ã使ã£ãã¢ã¯ã»ã¹ ããptintln strArray[0] ããptintln strList[0] // å é¨ã¤ãã¬ã¼ã¿ã¼ã使ã£ãã¢ã¯ã»ã¹ strArray.forEach { println it } strList.forEach { println it }
10.äºææ§ã®åé¡ã«å¶ç´ããã¦ãããã¨
Javaè¨èªã®å ´åã¯ãã¨ã³ã¿ã¼ãã©ã¤ãºç³»ã§æ©ãæ¡ç¨ãããã¨ãããã¨ãããã¾ãããWrite Once Run Anywhereã®ææ³ãããã®ããããã¾ããããéå»ã®ãã¼ã¸ã§ã³ã¨ã®äºææ§ã¨ããã®ãé常ã«å¤§åã«ãã¦ãã¾ããããã¯ãããæå³ã§Javaã®Good Partã ã¨æãã¾ãããäºææ§ã®åé¡ã«å¼ãã¥ããã¦å¾çºã®è¨èªã¨æ¯ã¹ã¦ä¸ä¾¿ãªé¨åãæ®ã£ã¦ãã¾ã£ã¦ããã¨ããã¨ãããããã¾ããã
ãããã£ã¦ãä»ã«ä»£æ¿æ段ãæä¾ããã¦ããã«ãããããããã¾ã ã«VectorãHashtableã¨ãã£ãã¬ã¬ã·ã¼ãªã¯ã©ã¹ããã£ã¨æ®ã£ã¦ããããåç¯ã§èª¬æããåæ¶å»ã«ããGenericsã®æ©è½å¶éãªã©ä¸ä¾¿ãªã¨ãããåºã¦ãã¦ãã¾ãã
ã¾ããJavaEEã§ã¯ã»ã¨ãã©ã ãã使ããªããããªAPIãæ®åãã¦ãããã¨ã«ããå®è£
ã大å¤ã«ãªã£ããä»æ§ãè¨ãä¸ãã£ã¦ãã¦ãã¾ããJavaEE6ã§ã¯ä»æ§ã®éå¼ãï¼Pruningï¼ã¨ããä»çµã¿ãã§ããã®ã§ãå°æ¥ã®ãã¼ã¸ã§ã³ã§ã¯æãåã£ã¦ä»æ§ãã¹ãªã åãããå¯è½æ§ãããã¾ããã
åæã«ãå¤ãã¦ä½¿ãããªããªã£ãAPIã«ã¤ãã¦ã¯ãä»å¾æ®µéçã«åé¤ãããã¨ãæ¤è¨ããã¦ãã¾ããããã¯ãã«ã¼ãã³ã°(Pruning)ã¨å¼ã°ãã¦ãããç¾æç¹ã§ã¯EJB 2.xã®ã¨ã³ãã£ãã£BeanãJAX-RPCãåè£ã«ä¸ãã£ã¦ãã¾ãã
Java EE 6 時代のアプリケーション開発: 第1回 Java EE 6概要と開発・実行環境の導入
ï¼è¿½è¨ï¼
æ¬ã¨ã³ããªã§ã¯Groovyãä¸å¿ã«ããæ¹åæ¡ãæ¸ãã¦ãã¾ãããid:xuweiãããScalaã使ã£ãæ¹æ³ã«ã¤ãã¦ã¾ã¨ãã¦ãã ãã£ã¦ãã¾ãã®ã§ãåèã«ãªãã¨æãã¾ããJavaの10個のBad Partsのほとんどはscalaだと解決されちゃうんだぜ - xuwei-k's blog
*1:Xstreamãªã©æè¿ã§ã¯XMLãæ±ã便å©ãªã©ã¤ãã©ãªã¼ãå©ç¨ã§ãããããDOMãSAXãç´æ¥ä½¿ãå¿ è¦æ§ã¯ã»ã¨ãã©ãªããªãã¾ããã
*2:åå¿è ã®äººã¯å®æã«ExceptionãThrowableã®å ¨ä½ããã£ããããã³ã¼ããæ¸ãã¦ãã¾ããã¨ãããã®ã§ãããããã§ã¯é©åãªä¾å¤å¦çããããªããªãã¨ãããã¨ã§éå¹æã«ãªã£ã¦ãã¾ãã¾ãã
*3:æ°ã«å ¥ã£ãæ¹æ³ããããããã¨ããã§å©ç¨ã§ããã¨æãè¾¼ããã¨ã
*4:ã³ã¼ãã£ã³ã°ä½æ¥ããé ããã£ã¦ããhands-offã¢ã¼ããã¯ãã¯ãå¤ãæç§æ¸ã®ä¾¡å¤è¦³ã«ç¸ããã¦ããããã£ãããã°ã©ãã¼ã®è¦ãã¿ãåãããªããã¨ãããã以ä¸ãåç §ãアーキテクトもプログラミングするべきか? - 達人プログラマーを目指して
*5:C#ãªã©.NETã®è¨èªã§ã¯usingæã¨ããã®ããããé¥ãã«ç°¡åã«è¨è¿°ã§ãã¾ãã
*6:å å¼è¨èªã§ããC#ã§ã¯ã©ããã¼åã¯ãªãã代ããã«int?ãªã©ã®nullableåããããScalaã ã¨nullã使ããOptionåã§è¡¨ç¾ã§ããã
*7:Java5ã§ã¯å宣è¨ã®é¨å以å¤ã§ã¯è¦ããä¸åºæ¬åãæ±ã£ã¦ããããã«ã³ã¼ãã£ã³ã°ã§ããããã ãList< int >ã®ãããªå®£è¨ã¯ã§ãããå®éã«Integerã«å¤æããã¦æ ¼ç´ãããããã®ç¹ã§ã¯C#ã®Genericsã¨å¤§ããç°ãªã£ã¦ããã
*8:ãã¨ãã°ãIntegeråã«nullãå ¥ã£ã¦ããããããèªåçã«intã«å¤æãããéã«NullPointerExceptionã«ãªã
*9:å¾ä»ããããGenericsã«è¨è¿°æ¹æ³ã¯è¿ãããæ¦å¿µçã«ã¯ã¾ã£ããç°ãªããã®ã§ãã
*10:ãã¤ãã³ã¼ãä¸ã«åãè¾¼ã¾ããåã¯reified typeã¨å¼ã°ãã¾ãã
*11:ãã®ç¹ã¯èª¤è§£ãããããã®ã§ãããç·ç§°åã宣è¨ãã¦å©ç¨ããå´ã®ã¯ã©ã¹å®ç¾©ä¸ã«ã¯Genericsã®åãã¡ã¿æ
å ±ã¨ãã¦åå¨ãã¦ãã¾ãããã¨ãã°ããã£ã¼ã«ãã®åãList
*12:ãã®å¶ç´ã«å¯¾ãã次åã®çã¨ãã¦Classã¯ã©ã¹ãã¿ã¤ããã¼ã¯ã³ã¨ãã¦å©ç¨ãã¦ããªã¯ã¬ã¯ã·ã§ã³APIã§ã¤ã³ã¹ã¿ã³ã¹ãçæãããã¨ãã§ãã¾ãã以ä¸ãåç §ãhttp://blog.isocchi.com/2009/02/new.html
*13:å
±å¤æ§ã¨ã¯é
åã®ããã«åãã©ã¡ã¼ã¿ã®ç¶æ¿é¢ä¿ã¨é
åèªèº«ã®åã®ç¶æ¿é¢ä¿ãåä¸ã§ãããã¨ãè¨ãã¾ããã¤ã¾ããChildãParentã®ãµãã¯ã©ã¹ã§ããã°Child[ ]ã¯Parent[ ]ã®ãµãã¯ã©ã¹ã¨ããé¢ä¿ã§ããããããã³ã¬ã¯ã·ã§ã³ã«ã¯å
±å¤æ§ããªããããList
*14:Javaã§ã¯é åã®å ±å¤æ§ã«ãããã³ã³ãã¤ã«æã«å®å ¨ã«ã¯åå®å ¨æ§ãä¿éã§ãã¾ããããã¨ãã°ãString[ ]ã®ã¤ã³ã¹ã¿ã³ã¹ãåç §ããé åãObject[ ]åã®å¤æ°ã«ä»£å ¥ãããå ´åãå®éã«Integerãè¨å®ã§ãã¦ãã¾ãã¾ãããå®è¡æã«ArrayStoreExceptionã«ãªãå¯è½æ§ãé²ãã¾ããã普通の(業務)Javaアプリケーションでは配列をなるべく使用しない方がよい - 達人プログラマーを目指して