JDK7在语法上的几处小变化

1 ,菱形语法(泛型实例化类型自动推断)

 

Java代码
  1. List<String> list =  new  ArrayList<>();  // <>这个真的很像菱形   
List<String> list = new ArrayList<>(); // <>这个真的很像菱形
 

2 ,在目前版本中,不可具体化的泛型(任意类型)可变参数,在编译时,会在调用处产生警告, JDK7 里将这个警告挪到了方法定义处。

变化前:

 

Java代码
  1. static  <T> List<T> asList(T... elements) { ... }  
  2. static  List<Callable<String>> stringFactories() {  
  3.     Callable<String> a, b, c;  
  4.     ...  
  5.      // 警告处   
  6.     return  asList(a, b, c);  
  7.   }  
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
     // 警告处
    return asList(a, b, c);
  }
 

变化后:

 

Java代码
  1. // 警告处   
  2. static  <T> List<T> asList(T... elements) { ... }  
  3. static  List<Callable<String>> stringFactories() {  
  4.     Callable<String> a, b, c;  
  5.     ...  
  6.     return  asList(a, b, c);  
  7.   }  
// 警告处
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    return asList(a, b, c);
  }
 

3 ,字符串终于可以 switch .

 

Java代码
  1. String s = ...  
  2. switch (s) {  
  3.   case   "quux" :  
  4.     processQuux(s); //没有break,继续往下   
  5.   
  6.   case   "foo" :  
  7.   case   "bar" :  
  8.     processFooOrBar(s);  
  9.     break ;  
  10.   case   "baz" :  
  11.      processBaz(s); //没有break,继续往下   
  12.   
  13.   default :  
  14.     processDefault(s);  
  15.     break ;  
  16. }  
String s = ...
switch(s) {
  case "quux":
    processQuux(s); //没有break,继续往下

  case "foo":
  case "bar":
    processFooOrBar(s);
    break;
  case "baz":
     processBaz(s); //没有break,继续往下

  default:
    processDefault(s);
    break;
}
 

4 ,支持二进制语法和单位级别的数字表示方式

 

Java代码
  1. // 8位byte   
  2. byte  aByte = ( byte )0b00100001;  
  3. // 16位short   
  4. short  aShort = ( short )0b1010000101000101;  
  5. // 32位int   
  6. int  anInt1 = 0b10100001010001011010000101000101;  
// 8位byte
byte aByte = (byte)0b00100001;
// 16位short
short aShort = (short)0b1010000101000101;
// 32位int
int anInt1 = 0b10100001010001011010000101000101;
 

支持单位级别的数字,提高可读性

 

Java代码
  1. long  underScores = 9_223_372_036_854_775_807L;  // 每三位加一下划线,等同于 9,223,372,036,854,775,807   
long underScores = 9_223_372_036_854_775_807L; // 每三位加一下划线,等同于 9,223,372,036,854,775,807
 

5 ,从语法层面上支持集合,不再是数组的专利。

 

Java代码
  1. final  List<Integer> piDigits = [ 314159265359 ];  
  2. final  Set<Integer> primes = {  27311278191131071524287  };  
  3. final  Map<Integer, String> platonicSolids = {  4  :  "tetrahedron" ,  
  4. 6  :  "cube"8  :  "octahedron"12  :  "dodecahedron"20  :  "icosahedron"   
  5. };  
final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
};
 

6 JSR 292 动态类型语言支持

 

Java代码
  1. Dynamic x = (动态语言脚本);  
  2. Object  y = x.foo("ABC" ).bar( 42 ).baz();  
Dynamic x = (动态语言脚本);
Object  y = x.foo("ABC").bar(42).baz();
 

7 ,动态资源管理

在目前版本的 java 中,当你操作流时,一定会加 try..finally 以保证出现异常时,流能被正确关闭。

 

Java代码
  1. BufferedReader br =  new  BufferedReader( new  FileReader(path));  
  2. try  {  
  3.     return  br.readLine();  
  4. finally  {  
  5.     br.close();  
  6. }  
BufferedReader br = new BufferedReader(new FileReader(path));
try {
    return br.readLine();
} finally {
    br.close();
}
 

JDK7 里,你只需要将资源定义在 try() 里, Java7 就会在 readLine 抛异常时,自动关闭资源。

另外,资源类必须实现 Disposable<?> 接口。支持管理多个资源。

 

Java代码
  1. try  (BufferedReader br =  new  BufferedReader( new  FileReader(path)) {  
  2.     return  br.readLine();  
  3. }  
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
    return br.readLine();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值