java 7快到来了,这里先窥探下几个新的实用的特性,简单明了了解下.
1)switch中,可以不再一定要用整型了,比如以前这样做:
switch(someInt) {
case 0: return zeroIntResult; break;
case 1: return oneIntResult; break;
default: return new BrokenSwitchException("Bad Integer");
}
现在可以这样了
switch(someString) {
case "Bubba": return bubbaStringResult; break;
case "Sue": return sueStringResult; break;
default: return new BrokenSwitchException("Odd String!");
}
2) TRY的改进
以前:
try {
} catch (AnyExceptionThrownByThatOp aetbto) {
// handle the exception byfm
} finally {
try {
} catch (Exception ignored) {
}
}
以前很麻烦,现在:
try (FileOutputStream fos = new FileOutputStream(file)) {
} catch (AnyExceptionThrownByThatOp aetbto) {
}
不再用finally了
3)多路TRY的合拼
以前:
try {
} catch (FirstSimilarException fse) {
} catch (SecondSimilarException sse) {
} catch (Exception ex) {
}
现在可以合拼了:
try {
} catch (FirstSimilarException | SecondSimilarException e) {
} catch (Exception ex) {
}
4) 简化范型的写法
以前:
Map<Integer, List<String>> aMap = new HashMap<Integer, List<String>>();
在JAVA 7中可以这样了:
Map<Integer, List<String>> aMap = new HashMap<>();
5) LONG的清晰显示
以前:
long creditCard = 5440444033300005L;
long phoneNumber = 2135551212L;
现在清晰的写法:
long creditCard = 5440_4440_3330_0005L;
long phoneNumber = 213_555_1212L;
1)switch中,可以不再一定要用整型了,比如以前这样做:
switch(someInt) {
case 0: return zeroIntResult; break;
case 1: return oneIntResult; break;
default: return new BrokenSwitchException("Bad Integer");
}
现在可以这样了
switch(someString) {
case "Bubba": return bubbaStringResult; break;
case "Sue": return sueStringResult; break;
default: return new BrokenSwitchException("Odd String!");
}
2) TRY的改进
以前:
try {
} catch (AnyExceptionThrownByThatOp aetbto) {
// handle the exception byfm
} finally {
try {
} catch (Exception ignored) {
}
}
以前很麻烦,现在:
try (FileOutputStream fos = new FileOutputStream(file)) {
} catch (AnyExceptionThrownByThatOp aetbto) {
}
不再用finally了
3)多路TRY的合拼
以前:
try {
} catch (FirstSimilarException fse) {
} catch (SecondSimilarException sse) {
} catch (Exception ex) {
}
现在可以合拼了:
try {
} catch (FirstSimilarException | SecondSimilarException e) {
} catch (Exception ex) {
}
4) 简化范型的写法
以前:
Map<Integer, List<String>> aMap = new HashMap<Integer, List<String>>();
在JAVA 7中可以这样了:
Map<Integer, List<String>> aMap = new HashMap<>();
5) LONG的清晰显示
以前:
long creditCard = 5440444033300005L;
long phoneNumber = 2135551212L;
现在清晰的写法:
long creditCard = 5440_4440_3330_0005L;
long phoneNumber = 213_555_1212L;