JDK1.7(jdk7.0)新特性

来源:互联网 发布:p2p网络搭建 编辑:程序博客网 时间:2024/05/24 05:27

1 对集合类的语言支持; 
2 自动资源管理; 
3 改进的通用实例创建类型推断; 
4 数字字面量下划线支持; 
5 switch中使用string; 
6 二进制字面量; 
7 简化可变参数方法调用


1.switch中可以使用字串了

String s = "test";switch (s) {case "test" :System.out.println("test");case "test1" :System.out.println("test1");break ;default :System.out.println("break");break ;}


2."<>"这个玩意儿的运用List<String> tempList = new ArrayList<>(); 即泛型实例化类型自动推断。
  泛型实例的创建可以通过类型推断来简化 可以去掉后面new部分的泛型类型,只用<>就可以了。
  //使用泛型前 
    List strList = new ArrayList(); 
    List<String> strList4 = new ArrayList<String>(); 
    List<Map<String, List<String>>> strList5 =  new ArrayList<Map<String, List<String>>>();
      
    //编译器使用尖括号 (<>) 推断类型 
    List<String> strList0 = new ArrayList<String>(); 
    List<Map<String, List<String>>> strList1 =  new ArrayList<Map<String, List<String>>>(); 
    List<String> strList2 = new ArrayList<>(); 
    List<Map<String, List<String>>> strList3 = new ArrayList<>();
    List<String> list = new ArrayList<>();
    list.add("A");
      // The following statement should fail since addAll expects
      // Collection<? extends String>
    //list.addAll(new ArrayList<>()); 

3.数值可加下划线
  数字类型的下划线表示 更友好的表示方式,不过要注意下划线添加的一些标准,可以参考下面的示例
 例如:
    long creditCardNumber = 1234_5678_9012_3456L;
    long socialSecurityNumber = 999_99_9999L;
    float pi = 3.14_15F;
    long hexBytes = 0xFF_EC_DE_5E;
    long hexWords = 0xCAFE_BABE;
    long maxLong = 0x7fff_ffff_ffff_ffffL;
    byte nybbles = 0b0010_0101;
    long bytes = 0b11010010_01101001_10010100_10010010; 
    //float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
    //float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point
    //long socialSecurityNumber1= 999_99_9999_L;         // Invalid; cannot put underscores prior to an L suffix 
    //int x1 = _52;              // This is an identifier, not a numeric literal
    int x2 = 5_2;              // OK (decimal literal)
    //int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
    int x4 = 5_______2;        // OK (decimal literal) 
    //int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix
    //int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number
    int x7 = 0x5_2;            // OK (hexadecimal literal)
    //int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number 
    int x9 = 0_52;             // OK (octal literal)
    int x10 = 05_2;            // OK (octal literal)
    //int x11 = 052_;            // Invalid; cannot put underscores at the end of a number 
    int one_million = 1_000_000;

4.支持二进制文字

  例如:int binary = 0b1001_1001;

5. 语法上支持集合,而不一定是数组
  
  final List<Integer> piDigits = [ 1,2,3,4,5,8 ];


6. 新增一些取环境信息的工具方法

File System.getJavaIoTempDir() // IO临时文件夹
File System.getJavaHomeDir() // JRE的安装目录
File System.getUserHomeDir() // 当前用户目录
File System.getUserDir() // 启动java进程时所在的目录
.......


7. Boolean类型反转,空指针安全,参与位运算

Boolean Booleans.negate(Boolean booleanObj)
True => False , False => True, Null => Null
boolean Booleans.and(boolean[] array)
boolean Booleans.or(boolean[] array)
boolean Booleans.xor(boolean[] array)
boolean Booleans.and(Boolean[] array)
boolean Booleans.or(Boolean[] array)
boolean Booleans.xor(Boolean[] array)


8. 两个char间的equals

boolean Character.equalsIgnoreCase(char ch1, char ch2)


9,安全的加减乘除

  int Math.safeToInt(long value)
  int Math.safeNegate(int value)
  long Math.safeSubtract(long value1, int value2)
  long Math.safeSubtract(long value1, long value2)
  int Math.safeMultiply(int value1, int value2)
  long Math.safeMultiply(long value1, int value2)
  long Math.safeMultiply(long value1, long value2)
  long Math.safeNegate(long value)
  int Math.safeAdd(int value1, int value2)
  long Math.safeAdd(long value1, int value2)
  long Math.safeAdd(long value1, long value2)
  int Math.safeSubtract(int value1, int value2)
 

10.对Java集合(Collections)的增强支持


  在JDK1.7之前的版本中,Java集合容器中存取元素的形式如下:
  以List、Set、Map集合容器为例:

    //创建List接口对象
    List<String> list=new ArrayList<String>();
    list.add("item"); //用add()方法获取对象
    String Item=list.get(0); //用get()方法获取对象

    //创建Set接口对象
    Set<String> set=new HashSet<String>();
    set.add("item"); //用add()方法添加对象

    //创建Map接口对象
    Map<String,Integer> map=new HashMap<String,Integer>();
    map.put("key",1); //用put()方法添加对象
    int value=map.get("key");

11.map集合支持并发请求,且可以写成 Map map = {name:"xxx",age:18};

  在JDK1.7中,摒弃了Java集合接口的实现类,如:ArrayList、HashSet和HashMap。而是直接采用[]、{}的形式存入对象,采用[]的形式按照索引、键值来获取集合中的对象,如下:

      List<String> list=["item"]; //向List集合中添加元素
      String item=list[0]; //从List集合中获取元素
      Set<String> set={"item"}; //向Set集合对象中添加元素
      Map<String,Integer> map={"key":1}; //向Map集合中添加对象
      int value=map["key"]; //从Map集合中获取对象

11.简化了可变参数方法的调用。传递非具体化参数(Non-Reifiable Formal Parameters)
  当程序员试图使用一个不可具体化的可变参数并调用一个*varargs*(可变)方法时,编辑器会生成一个“非安全操作”的警告。 
  JDK 7将警告从call转移到了方法声明(methord declaration)的过程中。这样API设计者就可以使用vararg,因为警告的数量大大减少了。

12.try-with-resources 语句
  jdk7提供了try-with-resources,可以自动关闭相关的资源(只要该资源实现了AutoCloseable接口,jdk7为绝大部分资源对象都实现了这个接口)
 
  static String readFirstLineFromFile(String path) throws IOException {  
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {  
      return br.readLine();  
    }  
  }  
  
  try 语句块中还可以同时处理多个资源,可以跟普通的try语句一样catch异常,有finally语句块
    try (  
      java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);  
      java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)  
    ) {  
      }catch(…){  
    }finally{  
  }  
 
13.Catch多个Exception,rethrow exception 改进了类型检测
 
   很多时候,我们捕获了多个异常,却做了相同的事情,比如记日志,包装成新的异常,然后rethrow。这时,代码就不那么优雅了,例如
  catch (IOException ex) {  
     logger.log(ex);  
     throw ex;  
  catch (SQLException ex) {  
     logger.log(ex);  
     throw ex;  
    }  
 Jdk7允许捕获多个异常
  catch (IOException|SQLException ex) {  
    logger.log(ex);  
    throw ex;  
}  
  注意,catch后面的异常参数是final的,不能重新再复制
 
  RethrowException更具包容性的类型检测
  当你重新抛出多个异常时,不再需要详细定义异常类型了,编译器已经知道你具体抛出的是哪个异常了。你只需在方法定义的时候声明需要抛出的异常即可
  

0 0
原创粉丝点击