2011-08-02è¿½è¨ JDK7ã«è´å½çãªãã°
éè¦ãªã®ã§å
é ã«è¿½è¨ãã¦ããã¾ãããJDK7ã®ãããã¹ãããã³ã³ãã¤ã©ã«è´å½çãªãã°ãåå¨ããããã§ãã
詳ããã¯ã³ãã©ã
http://d.hatena.ne.jp/takahashikzn/20110802
JDK7ã使ç¨ããéã¯ã注æä¸ããã
(Javadocã®ã¹ã¿ã¤ã«ãã¬ã©ã£ã¨å¤ããã¾ãã!!)
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
JDK7ã®æ£å¼çããªãªã¼ã¹ããã¦ãã¾ãã
èå³æ·±ãæ©è½ãAPIãªã©ã追å ããã¦ããããã§ããã
ãã®ä¸ã§ãã¿ããªä¸çªæ°ã«ãªãã¨ããã§ãããJavaè¨èªä»æ§ã®å¤æ´ã«ã¤ãã¦ãç´¹ä»ã
ãã¨ã¯ãå人çã«java.lang.invokeãªãã¦ã¢ã¤ã·ã²ãªããã±ã¼ã¸ãæ°ã«ãªã£ã¦ããã®ã§ããâ¦
APIãè¦ãéããåçè¨èªãµãã¼ãé¢é£ã®ããã±ã¼ã¸ã£ã½ãã§ãããã³ã¬ãã©ãªãã解説ããé¡ããã¾ã(ç¬)
ã§ã¯ãé 次è¦ã¦ããã¾ãããã
â»å¿µã®ããè¨è¼:大åã®ãµã³ãã«ã³ã¼ãã¯ãªãªã¼ã¹ãã¼ãããã®å¼ç¨(ä¸é¨ã翻訳ã®ããã«æ¹å¤)ã§ãããèä½æ¨©ã¯Oracleã«ããã¾ãã
Binary Literals (ãã¤ããªãªãã©ã«)
ããã¾ã§ã¯ããã¤ããªå¤ã®ãªãã©ã«ã使ãããã®ãªã
byte b = 0x42;
ãªã©ã¨æ¸ãå¿
è¦ããã£ãããã§ããã§ãããã ã¨ãããããã¿ã¼ã³ãã¤ã¡ã¼ã¸ãã¥ããã
(è³å
ã«ã16é²æ°â2é²æ°ãå¤æãã¼ãã¦ã§ã¢åè·¯ãç¼ææ¸ã¿ã®æ¹ãé¤ã)
ã¨ããããã§ãJDK7ããã¯ãããªé¢¨ã«æ¸ããã¨ãåºæ¥ãããã«ãªãã¾ããã
byte b = 0b01000010;
byteã ããããªãã¦ãä»ã®åã®ãã¤ããªè¡¨ç¾ãããã¾ãã
// An 8-bit 'byte' value: byte aByte = (byte)0b00100001; // A 16-bit 'short' value: short aShort = (short)0b1010000101000101; // Some 32-bit 'int' values: int anInt1 = 0b10100001010001011010000101000101; int anInt2 = 0b101; int anInt3 = 0B101; // The B can be upper or lower case. // A 64-bit 'long' value. Note the "L" suffix: long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
ãã¡ãããé
åã§ã使ãã¾ãã
public static final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001, 0b00010011, 0b00100110, 0b01001100, 0b10011000 };
æ´æ°åã®å®æ°ã使ããå ´æãªãã©ãã§ã使ãã¾ãã(ä¾ãã°caseã©ãã«)
public State decodeInstruction(int instruction, State state) { if ((instruction & 0b11100000) == 0b00000000) { final int register = instruction & 0b00001111; switch (instruction & 0b11110000) { case 0b00000000: return state.nop(); case 0b00010000: return state.copyAccumTo(register); case 0b00100000: return state.addToAccum(register); case 0b00110000: return state.subFromAccum(register); case 0b01000000: return state.multiplyAccumBy(register); case 0b01010000: return state.divideAccumBy(register); case 0b01100000: return state.setAccumFrom(register); case 0b01110000: return state.returnFromCall(); default: throw new IllegalArgumentException(); } } else { final int address = instruction & 0b00011111; switch (instruction & 0b11100000) { case 0b00100000: return state.jumpTo(address); case 0b01000000: return state.jumpIfAccumZeroTo(address); case 0b01000000: return state.jumpIfAccumNonzeroTo(address); case 0b01100000: return state.setAccumFromMemory(address); case 0b10100000: return state.writeAccumToMemory(address); case 0b11000000: return state.callTo(address); default: throw new IllegalArgumentException(); } } }
Underscores in Numeric Literals (ã¢ã³ãã¼ã¹ã³ã¢å ¥ãæ°å¤ãªãã©ã«)
æ°å¤ãªãã©ã«ã«ãå¯èªæ§ã®ããã«ã¢ã³ãã¼ã¹ã³ã¢ãå
¥ããããããã«ãªãã¾ããã
int i = 1_2_3;
ã¯ã
int i = 123;
ã¨åãæå³ã§ãã
ãã ããææ³ä¸ããããã¢ã³ãã¼ã¹ã³ã¢ãå
¥ããã¨å¥ã®æå³ã«ãªããã¨ãããã®ã§ã
ç´ããããã±ã¼ã¹ã§ã¯æ³¨æãå¿
è¦ã§ãã
ä¾ãã°ã
int i = _123;
ã¯
int i = 123;
ã¨ã¯éãã¾ãããã®ã¨ãã_123ã¯ãã ã®å¤æ°åã¨ãã¦æ±ããããã
int _123 = 999; int i = _123; // i == 999
ã¨ããæå³ã¨ãã¦è§£éããã¾ã(ããã¾ã§ã©ããã®ä»æ§)ã
Strings in switch Statements (switchæã®caseã©ãã«ã«æååã使ãã)
caseã©ãã«ã«æååãªãã©ã«ã使ããããã«ãªãã¾ããã
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay = "Start of work week"; break; case "Tuesday": case "Wednesday": case "Thursday": typeOfDay = "Midweek"; break; case "Friday": typeOfDay = "End of work week"; break; case "Saturday": case "Sunday": typeOfDay = "Weekend"; break; default: throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); } return typeOfDay; }
ã©ãã«ã®ãããã³ã°ã¯String#equalsã§è¡ããããããæååã¯ã±ã¼ã¹ä¾åã§ãã
ã§ãããã®ä½¿ãã©ããã§ããã
if("Monday".equals(dayOfWeekArg)) typeOfDay = "Start of work week"; else if ("Tuesday".equals(dayOfWeekArg)) typeOfday = "Wednesday"; else if...
ã¨æ¸ãããã¯ãå¤å°ãªãã¨ãå¹ççãªã³ã¼ãã¸ã¨ã³ã³ãã¤ã«ãã¦ãããããã§ãã
Type Inference for Generic Instance Creation (ã³ã³ã¹ãã©ã¯ã¿å¼ã³åºãæã®åæ¨è«)
å¾
æã®åæ¨è«ãå®è£
ããã¾ãããã¨è¨ã£ã¦ãã³ã³ã¹ãã©ã¯ã¿å¼ã³åºãã®æã ãã§ããã
ä¾ãã°ããããªã³ã¼ãã
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
ãããªé¢¨ã«æ¸ããã¨ãåºæ¥ãããã«ãªãã¾ãããã³ã¬ãè¶ ã©ã¯ãã³ã
Map<String, List<String>> myMap = new HashMap<>();
ã¡ãªã¿ã«ãã³ã¬ã¯ãã¡ã§ããåã«rawã¿ã¤ãè¦åãåºã¾ãã(å¾æ¥éãã®ä»æ§)
Map<String, List<String>> myMap = new HashMap();
ä¸è½ã§ã¯ãªãã®ã§ããã®ãããªã±ã¼ã¹ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã§ãã
List<String> list = new ArrayList<>(); list.add("A"); // å¼æ°ãCollection<? extends String>ã¨è§£éãã¦ãããªãã®ã§ã³ã³ãã¤ã«ã¨ã©ã¼ list.addAll(new ArrayList<>());
Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods (åãã©ã¡ã¼ã¿ã®å¯å¤é·å¼æ°ã«ãããã³ã³ãã¤ã©è¦åã®æ¹å)
åãã©ã¡ã¼ã¿ã®å¯å¤é·å¼æ°ã使ã£ã¦ããã¨ããã§ãã³ã³ãã¤ã©è¦åã®çºçå
·åãå¤ããã¾ããã
ãã¼ãæ±æã¨ã¯
ä¾ãã°ããã®ãããªã³ã¼ããæ¸ããã¨ãå¯è½ã§ãã
List l = new ArrayList<Number>(); List<String> ls = l; // æªãã§ãã¯è¦å(ãã®1) l.add(0, new Integer(42)); // æªãã§ãã¯è¦å(ãã®2) String s = ls.get(0); // ClassCastExceptionãçºç
ããã¯ä½ããã¦ãããã¨ããã¨ãè¦ããã«ããããäºã§ãã
List l = new ArrayList(); List ls = l; l.add(0, new Integer(42)); String s = (String) ls.get(0); // ClassCastExceptionãçºç
ããããã¼ãæ±æã¨å¼ã³ã¾ããã¸ã§ããªã¯ã¹ã使ã£ã¦åã®å®å
¨æ§ã
ä¿è¨¼ãã¦ããããã«è¦ãããã¦ãçå¼¾ãä»è¾¼ããã¨ãåºæ¥ãã¨ãããã¨ã§ãã
å¯å¤é·å¼æ°ã¨åãã©ã¡ã¼ã¿
以ä¸ã®ãããªã³ã¼ããèãã¦ã¿ã¾ãã
ã¸ã§ããªã¯ã¹ã®é
åã«ãã¼ãæ±æãä»è¾¼ãã§ããä¾ã§ãã
import java.util.*; public class ArrayBuilder { public static <T> void addToList (List<T> listArg, T... elements) { for (T x : elements) { listArg.add(x); } } public static void faultyMethod(List<String>... l) { Object[] objectArray = l; // ããã¾ã§ã¯æ£ãã objectArray[0] = Arrays.asList(new Integer(42)); // ãã¼ãæ±æããã String s = l[0].get(0); // ClassCastExceptionãçºç } } import java.util.*; public class HeapPollutionExample { public static void main(String[] args) { List<String> stringListA = new ArrayList<String>(); List<String> stringListB = new ArrayList<String>(); ArrayBuilder.addToList(stringListA, "Seven", "Eight", "Nine"); ArrayBuilder.addToList(stringListA, "Ten", "Eleven", "Twelve"); List<List<String>> listOfStringLists = new ArrayList<List<String>>(); ArrayBuilder.addToList(listOfStringLists, stringListA, stringListB); ArrayBuilder.faultyMethod(Arrays.asList("Hello!"), Arrays.asList("World!")); } }
JDK7ããã¯ãã³ã³ãã¤ã©ãArrayBuilder.addToListã§ä»¥ä¸ã®ãããªè¦åãåºãããã«ãªãã¾ããã
warning: [varargs] Possible heap pollution from parameterized vararg type T
è¦å: [å¯å¤é·å¼æ°] åãã©ã¡ã¼ã¿Tã®ãã¼ãæ±æãçºçããå¯è½æ§ãããã¾ãã
(訳注: å®éã«JDK7ã®ã³ã³ãã¤ã©ã§ãã®ã³ã¼ããã³ã³ãã¤ã«ãã¦ã¿ãããã§ã¯ãªãã®ã§ãã³ã³ãã¤ã©è¦åã®å訳ã¯ç°ãªããã)
ã§ããã®è¦åãåºããªãããã«ããããã«ã¯ã以ä¸ã®ããããã®æ段ã使ããã¨ã«ãªãã¾ãã
- æ°ãã追å ããã@SafeVarargsã使ã
- ä½ãstaticã¡ã½ããã§ãã使ãã¾ãã
- @SuppressWarnings({"unchecked", "varargs"})ã使ã
- ä½ãã¡ã½ããå¼ã³åºãå´ã®è¦åãæ¶ããã¨ã¯åºæ¥ã¾ãã
- ã³ã³ãã¤ã©ã®ãªãã·ã§ã³"-Xlint:varargs."ã使ã
以ä¸ã®ã³ã¼ããã³ã³ãã¤ã«ããæã
public class ArrayBuilder {public static
void addToList (List listArg, T... elements) {
for (T x : elements) {
listArg.add(x);
}
}@SuppressWarnings({"unchecked", "varargs"})
public staticvoid addToList2 (List listArg, T... elements) {
for (T x : elements) {
listArg.add(x);
}
}@SafeVarargs
public staticvoid addToList3 (List listArg, T... elements) {
for (T x : elements) {
listArg.add(x);
}
}// ...
}
public class HeapPollutionExample {// ...
public static void main(String[] args) {
// ...
ArrayBuilder.addToList(listOfStringLists, stringListA, stringListB);
ArrayBuilder.addToList2(listOfStringLists, stringListA, stringListB);
ArrayBuilder.addToList3(listOfStringLists, stringListA, stringListB);// ...
}
}
ã³ã³ãã¤ã«çµæã¯ä»¥ä¸ã®ã¨ããã§ãã
- addToList
- ã¡ã½ãã宣è¨ã§ã[unchecked] Possible heap pollution from parameterized vararg type Tã
- ã¡ã½ããå¼ã³åºãã§ã[unchecked] unchecked generic array creation for varargs parameter of type List
[]ã
- addToList2
- ã¡ã½ããå¼ã³åºãã§ã[unchecked] unchecked generic array creation for varargs parameter of type List
[]ã
- ã¡ã½ããå¼ã³åºãã§ã[unchecked] unchecked generic array creation for varargs parameter of type List
- addToList3
- (ä¸åè¦åãåºãã¾ãã)
The try-with-resources Statement (ãªã½ã¼ã¹ç®¡çç¨æ§æã®è¿½å )
å¿
ãã¯ãã¼ãºããªããã°ãªããªããªã½ã¼ã¹ã使ãå ´åãtry-finallyæ§æã使ãã®ããç´æã ã£ãããã§ããã
ã¡ãã£ã¨ä¾¿å©ãªæ§æã追å ããã¾ããã
static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }
è¤æ°ã®ãªã½ã¼ã¹ã使ãå ´åã¯ãããªæãã宣è¨ã¨ã¯éé ã«closeãå¼ã°ãã¾ãã
try ( java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) ) { // Enumerate each entry for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) { // Get the entry name and write it to the output file String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine; writer.write(zipEntryName, 0, zipEntryName.length()); } }
ãã®æ§æã§æå®ãããªã½ã¼ã¹ã¯ãjava.lang.AutoCloseableã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ããå¿
è¦ãããã¾ãã
ããã¯ãjava.io.Closeableã®ã¹ã¼ãã¼ã¤ã³ã¿ãã§ã¼ã¹ã§ãã
ã ããCloseableãå®è£
ãã¦ããã¯ã©ã¹ã¯ãæ¨æºã§ãã®æ§æã使ç¨å¯è½ã§ãã
Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking (è¤æ°ã®catchç¯ãä¾å¤ã®åã¹ãã¼ã«ã¤ãã¦ã®æ¹å)
è¤æ°ã®catchç¯ãæ¸ããããªã±ã¼ã¹ãæ¹åããã¾ããã
ä¾ãã°ããã®ãããªã³ã¼ãã
try { ... } catch (IOException ex) { logger.log(ex); throw ex; } catch (SQLException ex) { logger.log(ex); throw ex; }
ãããªé¢¨ã«æ¸ããããã«ãªãã¾ãã
try { ... } catch (IOException|SQLException ex) { logger.log(ex); throw ex; }
ã»ã¨ãã©ã®ã±ã¼ã¹ã§ã¯ãä¾å¤ããã£ãããã¦ãåºæ¥ããã¨ã¯éããã¦ããããã§ã
çµæã¨ãã¦catchç¯ã®ä¸èº«ã¯åããããªã³ã¼ãã«ãªããã¡ã§ãã
ã ãããã®æ§æãããã¨ãåé·ãªã³ã¼ããæ¸ãããã¨ãåºæ¥ãããã
ã¤ãã§ã«ãã³ã³ãã¤ã©ãå°ç¨ã®ã³ã¼ããçæãã¦ãããããã§ãcatchç¯ã並ã¹ã¦æ¸ããããå¦çãå¹ççã«ãªãããã§ãã
ä¾å¤åã¹ãã¼æã®åæ¨è«
JDK7ãããä¾å¤åã¹ãã¼æã«åæ¨è«ãåãããã«ãªãã¾ããã
// FirstException, SecondExceptionå ±ã«ãã§ãã¯ä¾å¤ public void rethrowException(String exceptionName) throws FirstException, SecondException { try { // FirstExceptionãSecondExceptionã ããçºçãããä½ããã®ã³ã¼ã } catch (Exception e) { throw e; } }
ãã®ã³ã¼ããJDK6ã¾ã§ã¯å½ç¶ã³ã³ãã¤ã«ã¨ã©ã¼ã§ãããJDK7ããã¯ã³ã³ãã¤ã«å¯è½ã
è¦ããã«ãtryç¯ã¨ããã«å¯¾å¿ããcatchç¯ã®ä¸èº«ãè¦ã¦ãthrows宣è¨ã¨ãã¦é©åãªåãå¤æãã¦ãããã¨ãããã¨ã§ãã
以ä¸ã®ã±ã¼ã¹ã§ãã®åæ¨è«ãæ©è½ãã¾ãã
- tryç¯ãæããå¯è½æ§ã®ããä¾å¤ã¯ãthorwsç¯ã§å®£è¨ãã¦ããä¾å¤ã ãã§ãã
- åæ¨è«ãé©ç¨ãããcatchç¯ã®åã«ãå ã«ä¾å¤ããã£ãããã¦ãã¾ãcatchç¯ãç¡ã
- throwsç¯ã§å®£è¨ããä¾å¤ã¯ãcatchç¯ã§å®£è¨ããä¾å¤ã®ãµãã¯ã©ã¹ã§ãã
ã¸ããä½ã ãé¢ç½ãæ©è½ã§ã¯ããã¾ããããã
ããããtry-catchã使ããªããããããããï¼ã¨ããæ°ãããªãã§ããªãã§ãã
éã«æ··ä¹±ãæãããããªãããã³ã¬ï¼