java 7 中的新特性

发布于 2024-07-07 16:14:51 字数 34 浏览 11 评论 0原文

java 7 将实现哪些新功能? 他们现在在做什么?

What new features in java 7 is going to be implemented?
And what are they doing now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

嗼ふ静 2024-07-14 16:14:51

Java SE 7 JDK 7 发行说明中的​​功能和增强功能

这是 OpenJDK 7 功能页面 中的 Java 7 新功能摘要:

vm JSR 292:支持动态类型语言(InvokeDynamic) 
          严格的类文件检查 
  lang JSR 334:小语言增强功能(Project Coin) 
  core 升级类加载器架构 
          关闭 URLClassLoader 的方法 
          并发和集合更新 (jsr166y) 
  国际化统一码 6.0 
          区域设置增强 
          单独的用户区域设置和用户界面区域设置 
  ionet JSR 203:更多适用于 Java 平台的新 I/O API (NIO.2) 
          用于 zip/jar 档案的 NIO.2 文件系统提供程序 
          SCTP(流控制传输协议) 
          SDP(套接字直接协议) 
          使用 Windows Vista IPv6 堆栈 
          传输层安全协议1.2 
  秒 椭圆曲线加密 (ECC) 
  JDBC 4.1 
  Java 2D 的客户端 XRender 管道 
          为 6u10 图形功能创建新的平台 API 
          Swing 的 Nimbus 外观和感觉 
          Swing JLayer 组件 
          Gervill 声音合成器 [新] 
  web 更新 XML 堆栈 
  mgmt 增强型 MBean [已更新] 
   

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;

switch 中的字符串

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 SE 7 Features and Enhancements from JDK 7 Release Notes

This is the Java 7 new features summary from the OpenJDK 7 features page:

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]

Code examples for new features in Java 1.7

Try-with-resources statement

this:

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

becomes:

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

You can declare more than one resource to close:

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

Underscores in numeric literals

int one_million = 1_000_000;

Strings in switch

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

Binary literals

int binary = 0b1001_1001;

Improved Type Inference for Generic Instance Creation

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

becomes:

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

Multiple exception catching

this:

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

becomes:

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

SafeVarargs

this:

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

becomes:

@SafeVarargs
public static void printAll(List<String>... lists){
    for(List<String> list : lists){
        System.out.println(list);
    }
}
野生奥特曼 2024-07-14 16:14:51

Java标准版(JSE 7)的新功能

  1. 用JLayer类装饰组件:

    JLayer 类是 Swing 组件的灵活而强大的装饰器。 Java SE 7 中的 JLayer 类在本质上与 java.net 上的 JxLayer 项目类似。 JLayer 类最初基于 JXLayer 项目,但其 API 是单独发展的。

  2. switch 语句中的字符串

    在 JDK 7 中,我们可以在 switch 语句的表达式中使用 String 对象。 Java 编译器从使用 String 对象的 switch 语句生成的字节码通常比从链式 if-then-else 语句生成的字节码更有效。

  3. 通用实例的类型推断:

    只要编译器可以从上下文推断类型参数,我们就可以用一组空的类型参数 (<>) 替换调用泛型类的构造函数所需的类型参数。 这对尖括号非正式地称为菱形。
    Java SE 7 支持通用实例创建的有限类型推断; 仅当构造函数的参数化类型从上下文中显而易见时,才可以使用类型推断。 例如,以下示例无法编译:

    列表<字符串>   l = new ArrayList<>(); 
      l.add("A"); 
      l.addAll(new ArrayList<>()); 
      

    相比之下,以下示例可以编译:

    列表   list2 = new ArrayList<>(); 
      l.addAll(list2); 
      
  4. 使用改进的类型检查捕获多个异常类型并重新抛出异常:

    在 Java SE 7 及更高版本中,单个 catch 块可以处理多种类型的异常。 此功能可以减少代码重复。 考虑以下代码,其中每个 catch 块中都包含重复的代码:

    catch (IOException e) { 
          记录器.log(e); 
          扔 e; 
      } 
      catch (SQLException e) { 
          记录器.log(e); 
          扔 e; 
      } 
      

    在 Java SE 7 之前的版本中,由于变量 e 具有不同的类型,因此很难创建通用方法来消除重复代码。
    以下示例在 Java SE 7 及更高版本中有效,消除了重复的代码:

    catch (IOException|SQLException e) { 
          记录器.log(e); 
          扔 e; 
      } 
      

    catch 子句指定块可以处理的异常类型,每个异常类型用竖线 (|) 分隔。

  5. java.nio.file包

    java.nio.file 包及其相关包 java.nio.file.attribute 为文件 I/O 和访问文件系统提供全面的支持。 JDK 7 中还提供了 zip 文件系统提供程序。

来源:http://ohmjavaclasses.blogspot.com/

New Feature of Java Standard Edition (JSE 7)

  1. Decorate Components with the JLayer Class:

    The JLayer class is a flexible and powerful decorator for Swing components. The JLayer class in Java SE 7 is similar in spirit to the JxLayer project project at java.net. The JLayer class was initially based on the JXLayer project, but its API evolved separately.

  2. Strings in switch Statement:

    In the JDK 7 , we can use a String object in the expression of a switch statement. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

  3. Type Inference for Generic Instance:

    We can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond.
    Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile:

    List<String> l = new ArrayList<>();
    l.add("A");
    l.addAll(new ArrayList<>());
    

    In comparison, the following example compiles:

    List<? extends String> list2 = new ArrayList<>();
    l.addAll(list2);
    
  4. Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking:

    In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication. Consider the following code, which contains duplicate code in each of the catch blocks:

    catch (IOException e) {
        logger.log(e);
        throw e;
    }
    catch (SQLException e) {
        logger.log(e);
        throw e;
    }
    

    In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable e has different types.
    The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

    catch (IOException|SQLException e) {
        logger.log(e);
        throw e;
    }
    

    The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

  5. The java.nio.file package

    The java.nio.file package and its related package, java.nio.file.attribute, provide comprehensive support for file I/O and for accessing the file system. A zip file system provider is also available in JDK 7.

Source: http://ohmjavaclasses.blogspot.com/

攒一口袋星星 2024-07-14 16:14:51

除了 John Skeet 所说的之外,这里还有 Java 7 项目概述。 它包括功能的列表和描述。

注意:JDK 7 于 2011 年 7 月 28 日发布,因此您现在应该访问官方 Java SE 站点

In addition to what John Skeet said, here's an overview of the Java 7 project. It includes a list and description of the features.

Note: JDK 7 was released on July 28, 2011, so you should now go to the official java SE site.

吹泡泡o 2024-07-14 16:14:51

语言变更

-Project Coin (small changes)
-switch on Strings
-try-with-resources
-diamond operator

库变更

-new abstracted file-system API (NIO.2) (with support for virtual filesystems)
-improved concurrency libraries
-elliptic curve encryption
-more incremental upgrades

平台变更

-support for dynamic languages

下面是解释 JAVA 7 新添加功能的链接,解释非常清楚,每个功能可能的小示例:

http://radar.oreilly.com/2011/ 09/java7-features.html

Language changes:

-Project Coin (small changes)
-switch on Strings
-try-with-resources
-diamond operator

Library changes:

-new abstracted file-system API (NIO.2) (with support for virtual filesystems)
-improved concurrency libraries
-elliptic curve encryption
-more incremental upgrades

Platform changes:

-support for dynamic languages

Below is the link explaining the newly added features of JAVA 7 , the explanation is crystal clear with the possible small examples for each features :

http://radar.oreilly.com/2011/09/java7-features.html

╰つ倒转 2024-07-14 16:14:51

使用 Diamond(<>) 运算符创建通用实例

Map<String, List<Trade>> trades = new TreeMap <> ();

在 switch 语句中使用字符串

String status=  “something”;
   switch(statue){
     case1: 
     case2: 
     default:
    }

数字文字中的下划线

int val 12_15;
长电话号码 = 01917_999_720L;

使用单个 catch 语句通过“|”抛出多个异常 运算符

catch(IOException | NullPointerException ex){
          ex.printStackTrace();   
    }

不需要 close() 资源,因为 Java 7 提供了 try-with-resources 语句

try(FileOutputStream fos = new FileOutputStream("movies.txt");
      DataOutputStream dos = new DataOutputStream(fos)) {
              dos.writeUTF("Java 7 Block Buster");
  } catch(IOException e) {
        // log the exception
  }

带有前缀“0b”或“0B”的二进制文字

Using Diamond(<>) operator for generic instance creation

Map<String, List<Trade>> trades = new TreeMap <> ();

Using strings in switch statements

String status=  “something”;
   switch(statue){
     case1: 
     case2: 
     default:
    }

Underscore in numeric literals

int val 12_15;
long phoneNo = 01917_999_720L;

Using single catch statement for throwing multiple exception by using “|” operator

catch(IOException | NullPointerException ex){
          ex.printStackTrace();   
    }

No need to close() resources because Java 7 provides try-with-resources statement

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”

℉絮湮 2024-07-14 16:14:51

我认为 ForkJoinPool 以及 Executor Framework 的相关增强是 Java 7 中的一个重要补充。

I think ForkJoinPool and related enhancement to Executor Framework is an important addition in Java 7.

可是我不能没有你 2024-07-14 16:14:51

以下列表包含 Java SE 7 中增强功能页面的链接。

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

The following list contains links to the the enhancements pages in the Java SE 7.

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

Reference 1 Reference 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文