New features in java 7
Java 7中的哪些新特性将被实现?他们现在在做什么?
JavaSE 7的特性和JDK 7发行说明的改进
这是来自OpenJDK 7特性页面的Java 7新特性摘要:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 vm JSR 292: Support for dynamically-typed languages (InvokeDynamic)
Strict class-file checking
lang JSR 334: Small language enhancements (Project Coin)
core Upgrade class-loader architecture
Method to close a URLClassLoader
Concurrency and collections updates (jsr166y)
i18n Unicode 6.0
Locale enhancement
Separate user locale and user-interface locale
ionet JSR 203: More new I/O APIs for the Java platform (NIO.2)
NIO.2 filesystem provider for zip/jar archives
SCTP (Stream Control Transmission Protocol)
SDP (Sockets Direct Protocol)
Use the Windows Vista IPv6 stack
TLS 1.2
sec Elliptic-curve cryptography (ECC)
jdbc JDBC 4.1
client XRender pipeline for Java 2D
Create new platform APIs for 6u10 graphics features
Nimbus look-and-feel for Swing
Swing JLayer component
Gervill sound synthesizer [NEW]
web Update the XML stack
mgmt Enhanced MBeans [UPDATED]
Java 1.7中新特性的代码示例Try with Resources语句
这是:
1 2 3 4 5 6 | BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); } |
变成:
1 2 3 |
可以声明多个要关闭的资源:
1 2 3 4 5 6 | try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { // code } |
数字文本中的下划线
1 | int one_million = 1_000_000; |
开关中的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | String s = ... switch(s) { case"quux": processQuux(s); // fall-through case"foo": case"bar": processFooOrBar(s); break; case"baz": processBaz(s); // fall-through default: processDefault(s); break; } |
二进制文字
1 | int binary = 0b1001_1001; |
用于创建通用实例的改进类型推理
变成:
1 |
多个异常捕获
这是:
1 2 3 4 5 6 7 | } catch (FirstException ex) { logger.error(ex); throw ex; } catch (SecondException ex) { logger.error(ex); throw ex; } |
变成:
1 2 3 4 | } catch (FirstException | SecondException ex) { logger.error(ex); throw ex; } |
萨法瓦拉克
这是:
1 2 3 4 5 6 | @SuppressWarnings({"unchecked","varargs"}) public static void printAll(List<String>... lists){ for(List<String> list : lists){ System.out.println(list); } } |
变成:
1 2 3 4 5 6 | @SafeVarargs public static void printAll(List<String>... lists){ for(List<String> list : lists){ System.out.println(list); } } |
Java标准版(JSE 7)的新特性
用jlayer类装饰组件:
jlayer类是一个灵活而强大的Swing组件装饰器。JavaSE 7中的JA级类与Java.NET中的JxLead项目项目类似。jlayer类最初是基于jxlayer项目的,但其API是独立发展的。
switch语句中的字符串:
在JDK7中,我们可以在switch语句的表达式中使用字符串对象。Java编译器从使用字符串对象的开关语句生成的字节码通常比链式IF语句更有效。
泛型实例的类型推断:
只要编译器可以从上下文推断类型参数,我们就可以用一组空的类型参数(<>)替换调用泛型类的构造函数所需的类型参数。这对尖括号非正式地称为菱形。JavaSE 7支持通用实例创建的有限类型推理;如果上下文中的构造函数的参数化类型是显而易见的,则只能使用类型推断。例如,以下示例不编译:
1 2 3 | List<String> l = new ArrayList<>(); l.add("A"); l.addAll(new ArrayList<>()); |
相比之下,以下示例编译:
1 2 | List<? extends String> list2 = new ArrayList<>(); l.addAll(list2); |
捕获多个异常类型并通过改进的类型检查重新引发异常:
在JavaSE 7和以后,单个catch块可以处理多个类型的异常。此功能可以减少代码重复。考虑下面的代码,它在每个catch块中包含重复的代码:
1 2 3 4 5 6 7 8 | catch (IOException e) { logger.log(e); throw e; } catch (SQLException e) { logger.log(e); throw e; } |
在Java SE 7之前的版本中,由于变量E具有不同的类型,所以很难创建一种消除重复代码的通用方法。下面的示例在Java SE 7和以后有效,消除了重复代码:
1 2 3 4 |
catch子句指定块可以处理的异常类型,并且每个异常类型用竖线()分隔。
java.nio.file包
资料来源:http://ohmjavaclasses.blogspot.com/
Java编程语言增强
官方参考Java8官方参考维基参考文献
除了John Skeet所说的,这里是Java 7项目的概述。它包括功能列表和描述。
注:JDK 7于2011年7月28日发布,所以您现在应该去官方的Java SE网站。
语言更改:
1 2 3 4 | -Project Coin (small changes) -switch on Strings -try-with-resources -diamond operator |
库更改:
1 2 3 4 | -new abstracted file-system API (NIO.2) (with support for virtual filesystems) -improved concurrency libraries -elliptic curve encryption -more incremental upgrades |
平台更改:
1 | -support for dynamic languages |
下面是解释Java 7新添加的特性的链接,解释很清楚,每个特征都有可能的小例子:
http://radar.oreilly.com/2011/09/java7-features.html
Using Diamond(<>) operator for generic instance creation
Using strings in switch statements
1 2 3 4 5 6 |
Underscore in numeric literals
In Val1215;长电话号码=01917_999_720l;
Using single catch statement for throwing multiple exception by using"|" operator
1 2 3 |
No need to close() resources because Java 7 provides try-with-resources statement
1 2 3 4 5 6 | try(FileOutputStream fos = new FileOutputStream("movies.txt"); DataOutputStream dos = new DataOutputStream(fos)) { dos.writeUTF("Java 7 Block Buster"); } catch(IOException e) { // log the exception } |
binary literals with prefix"0b" or"0B"
下面的列表包含指向Java SE 7中的增强页的链接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Swing IO and New IO Networking Security Concurrency Utilities Rich Internet Applications (RIA)/Deployment Requesting and Customizing Applet Decoration in Dragg able Applets Embedding JNLP File in Applet Tag Deploying without Codebase Handling Applet Initialization Status with Event Handlers Java 2D Java XML – JAXP, JAXB, and JAX-WS Internationalization java.lang Package Multithreaded Custom Class Loaders in Java SE 7 Java Programming Language Binary Literals Strings in switch Statements The try-with-resources Statement Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking Underscores in Numeric Literals Type Inference for Generic Instance Creation Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods Java Virtual Machine (JVM) Java Virtual Machine Support for Non-Java Languages Garbage-First Collector Java HotSpot Virtual Machine Performance Enhancements JDBC |
参考1参考2