• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Enhancements in Java 增强特性

互联网 diligentman 2周前 (04-09) 13次浏览

Enhancements in Java 增强特性,Java Language Changes for Java SE 9,分别介绍了Java 5至Java 9的增强特性,这里继续介绍后续版本的新特性。

JDK 10:移除了javah

  • 286:Local-Variable Type Inference,本地变量类型推断
    var list = new ArrayList<String>();  // infers ArrayList<String>
    var stream = list.stream();          // infers Stream<String>

JDK11:2018/09/25,移除了javaee和corba,支持TLS 1.3,废弃Nashorn和Pack200

  • 321:Http Client,http客户端,intro
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
          .uri(URI.create(http://openjdk.java.net/))
          .timeout(Duration.ofMinutes(1))
          .header(“Content-Type”, “applic++ation/json”)
          .POST(BodyPublishers.ofFile(Paths.get(“file.json”)))
          .build();
    HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
    c++lient.sendAsync(request, BodyHandlers.ofString())
          .thenApply(response -> { System.out.println(response.statusCode()); return response; } )
          .thenApply(HttpResponse::body)
          .thenAccept(System.out::println);
    //BodyPublishers::ofByteArray(byte[]) ofString(String) ofInputStream(Supplier<InputStream>)
    //BodyHandlers::ofByteArray() ofFile(Path) discarding()
  • 323:Local-Variable Syntax for Lambda Parameters
    (var x, var y) -> x.process(y)
  • 330:Launch Single-File Source-Code Programs,执行单源码文件
    java HelloWorld.java

JDK12:

  • 325:Switch Expressions (Preview),多条语句时使用花括号和break
    int numLetters = switch (day) {
         case MONDAY, FRIDAY, SUNDAY -> 6;
         case TUESDAY                -> 7;
         case THURSDAY, SATURDAY     -> 8;
         case WEDNESDAY              -> { break 9; }
    };

JDK13:改进Socket,

  • 354:Switch Expressions (Second Preview),使用yield(替代break)返回值
    int i = switch(day){
      default –> yield 0;
    }
  • 355:Text Blocks (Preview),文本块适用于html、sqljavascript
    String html = “””
                   <html>
                       <body>
                           <p>Hello, world</p>
                       </body>
                   </html>
                   “””;
    String query = “””
                    SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
                    WHERE `CITY` = ‘INDIANAPOLIS’
                    ORDER BY `EMP_ID`, `LAST_NAME`;
                    “””;
    ScriptEngine engine = new ScriptEngineManager().getEngineByName(“js”);
    Object obj = engine.eval(“””
                              function hello() {
                                  print(‘”Hello, world”‘);
                              }
                             
                              hello();
                              “””);

JDK14:2020/03/17,新增jpackage,转正switch,移除Pack200,

  • 305:Pattern Matching for instanceof (Preview),类型判断并声明变量
    if (obj instanceof String s && s.length() > 5) {
         // can use s here s.contains(..)
    } else {
         // can’t use s here
    }
    @Override public boolean equals(Object o) {
         return (o instanceof CaseInsensitiveString cis) &&
             cis.s.equalsIgnoreCase(s);
    }
  • 358:Helpful NullPointerExceptions
    a.i = 99; //a为null时错误信息更加详细
    Exception in thread “main” java.lang.NullPointerException:
             Cannot assign field “i” because “a” is null
         at Prog.main(Prog.java:5)
  • 359:Records (Preview),替代lombok,自动提供方法getter constructor equals hashCode toString,不可变更对象
    record Point(int x, int y) { }
    boolean Class#isRecord()
  • 370:Foreign-Memory Access API (Incubator),堆外内存访问
    VarHandle intHandle = MemoryHandles.varHandle(int.class,
             ByteOrder.nativeOrder());
    VarHandle intElemHandle = MemoryHandles.withStride(intHandle, 4);
    try (MemorySegment segment = MemorySegment.allocateNative(100)) {
         MemoryAddress base = segment.baseAddress();
         for (int i = 0; i < 25; i++) {
             intElemHandle.set(base, (long) i, i);
         }
    }

JDK15:移除Nashorn,改进DatagramSocket,

  • 339:Edwards-Curve Digital Signature Algorithm (EdDSA)
  • 360:Sealed Classes (Preview)
  • 371:Hidden Classes

JDK16:支持C++14语法,迁移至GitHub,

  • 338:Vector API (Incubator)

展开阅读全文

© 著作权归作者所有

举报

打赏

0


0 收藏

微信
QQ
微博

分享

作者的其它热门文章

metaweblog接口转发博客
LogStation 支持浏览器实时查看日志
Oracle数据库c++lob字段导出为sql insert插入语句
银行卡信息查询接口


程序员灯塔
转载请注明原文链接:Enhancements in Java 增强特性
喜欢 (0)