gpt4 book ai didi

java-7 - Java 7 中的新特性

转载 作者:IT老高 更新时间:2023-10-28 11:38:52 25 4
gpt4 key购买 nike

Java 7 中将实现哪些新特性?他们现在在做什么?

最佳答案

Java SE 7 Features and Enhancements来自 JDK 7 发行说明

这是来自 OpenJDK 7 features page 的 Java 7 新特性摘要:

vm  JSR 292: Support for dynamically-typed languages (InvokeDynamic)        Strict class-file checkinglang    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 localeionet   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.2sec     Elliptic-curve cryptography (ECC)jdbc    JDBC 4.1client  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 stackmgmt    Enhanced MBeans [UPDATED]

Java 1.7 中新功能的代码示例

Try-with-resources 语句

这个:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}

变成:

try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}

您可以声明多个要关闭的资源:

try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}

数字文字中的下划线

int one_million = 1_000_000;

开关中的字符串

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;
}

二进制字面量

int binary = 0b1001_1001;

改进了通用实例创建的类型推断

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

变成:

Map<String, List<String>> anagrams = new HashMap<>();

多个异常捕获

这个:

} catch (FirstException ex) {
logger.error(ex);
throw ex;
} catch (SecondException ex) {
logger.error(ex);
throw ex;
}

变成:

} catch (FirstException | SecondException ex) {
logger.error(ex);
throw ex;
}

SafeVarargs

这个:

@SuppressWarnings({"unchecked", "varargs"})
public static void printAll(List<String>... lists){
for(List<String> list : lists){
System.out.println(list);
}
}

变成:

@SafeVarargs
public static void printAll(List<String>... lists){
for(List<String> list : lists){
System.out.println(list);
}
}

关于java-7 - Java 7 中的新特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/213958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com