java功能模块

Java 13 was released for production use on 17th September 2019. There are not a lot of developer-specific features in Java 13 because of the 6-month release cycle.

Java 13已于2019年9月17日发布供生产使用。由于6个月的发布周期,Java 13中没有很多针对开发人员的功能。

Java 13功能 (Java 13 Features)

Some of the important Java 13 features are:

Java 13的一些重要功能包括:

  • Text Blocks – JEP 355文字块– JEP 355
  • New Methods in String Class for Text Blocks文本块的字符串类中的新方法
  • Switch Expressions Enhancements – JEP 354开关表达式增强功能– JEP 354
  • Reimplement the Legacy Socket API – JEP 353重新实现旧版套接字API – JEP 353
  • Dynamic CDS Archive – JEP 350动态CDS存档– JEP 350
  • ZGC: Uncommit Unused Memory – JEP 351ZGC:取消提交未使用的内存– JEP 351
  • FileSystems.newFileSystem() MethodFileSystems.newFileSystem()方法
  • Support for Unicode 12.1支持Unicode 12.1
  • DOM and SAX Factories with Namespace Support支持命名空间的DOM和SAX工厂

如何启用预览功能 (How to Enable Preview Features)

Switch expressions and text blocks are preview features. So you will have to enable the preview-feature settings in your project.

开关表达式和文本块是预览功能。 因此,您将必须在项目中启用预览功能设置。

If you are running a java program from the command line, you can enable it using the --enable-preview switch. You can use this switch to start JShell with preview features enabled.

如果您从命令行运行Java程序,则可以使用--enable-preview开关启用它。 您可以使用此开关在启用预览功能的情况下启动JShell。

$ jshell --enable-preview$ java --enable-preview --source 13 Test.java

If you are using Eclipse IDE, you can enable the preview features from the project Java Compiler settings.

如果使用的是Eclipse IDE,则可以从项目Java编译器设置中启用预览功能。

Eclipse Enable Preview Features

Eclipse启用预览功能

1.文字块– JEP 355 (1. Text Blocks – JEP 355)

This is a preview feature. It allows us to create multiline strings easily. The multiline string has to be written inside a pair of triple-double quotes.

这是预览功能。 它使我们可以轻松地创建多行字符串。 多行字符串必须写在一对三重双引号内。

The string object created using text blocks has no additional properties. It’s an easier way to create multiline strings. We can’t use text blocks to create a single-line string.

使用文本块创建的字符串对象没有其他属性。 这是一种创建多行字符串的简便方法。 我们不能使用文本块来创建单行字符串。

The opening triple-double quotes must be followed by a line terminator.

开头的三重双引号后必须跟一个行终止符。

package com.journaldev.java13.examples;public class TextBlockString {/*** JEP 355: Preview Feature*/@SuppressWarnings("preview")public static void main(String[] args) {String textBlock = """HiHelloYes""";String str = "Hi\nHello\nYes";System.out.println("Text Block String:\n" + textBlock);System.out.println("Normal String Literal:\n" + str);System.out.println("Text Block and String Literal equals() Comparison: " + (textBlock.equals(str)));System.out.println("Text Block and String Literal == Comparison: " + (textBlock == str));}}

Output:

输出:

Text Block String:
Hi
Hello
Yes
Normal String Literal:
Hi
Hello
Yes
Text Block and String Literal equals() Comparison: true
Text Block and String Literal == Comparison: true

It’s useful in easily creating HTML and JSON strings in our Java program.

在我们的Java程序中轻松创建HTML和JSON字符串很有用。

String textBlockHTML = """<html><head><link href='/css/style.css' rel='stylesheet' /></head><body><h1>Hello World</h1></body></html>""";String textBlockJSON = """{"name":"Pankaj","website":"JournalDev"}""";

2.文本块的字符串类中的新方法 (2. New Methods in String Class for Text Blocks)

There are three new methods in the String class, associated with the text blocks feature.

String类中有三个与文本块功能关联的新方法。

  1. formatted(Object… args): it’s similar to the String format() method. It’s added to support formatting with the text blocks.formatted(Object…args):它类似于String format()方法。 添加它是为了支持使用文本块进行格式化。
  2. stripIndent(): used to remove the incidental white space characters from the beginning and end of every line in the text block. This method is used by the text blocks and it preserves the relative indentation of the content.stripIndent():用于从文本块中的每一行的开头和结尾删除附带的空格字符。 文本块使用此方法,并保留内容的相对缩进。
  3. translateEscapes(): returns a string whose value is this string, with escape sequences translated as if in a string literal.translateEscapes():返回一个值为该字符串的字符串,其转义序列就像在字符串文字中一样进行翻译。
package com.journaldev.java13.examples;public class StringNewMethods {/**** New methods are to be used with Text Block Strings* @param args*/@SuppressWarnings("preview")public static void main(String[] args) {String output = """Name: %sPhone: %dSalary: $%.2f""".formatted("Pankaj", 123456789, 2000.5555);System.out.println(output);String htmlTextBlock = "<html>   \n"+"\t<body>\t\t \n"+"\t\t<p>Hello</p>  \t \n"+"\t</body> \n"+"</html>";System.out.println(htmlTextBlock.replace(" ", "*"));System.out.println(htmlTextBlock.stripIndent().replace(" ", "*"));String str1 = "Hi\t\nHello' \" /u0022 Pankaj\r";System.out.println(str1);System.out.println(str1.translateEscapes());}}

Output:

输出:

Name: Pankaj
Phone: 123456789
Salary: $2000.56<html>***<body>     *<p>Hello</p>** *</body>*
</html>
<html><body><p>Hello</p></body>
</html>
Hi
Hello' " /u0022 Pankaj
Hi
Hello' " /u0022 Pankaj

3.开关表达式增强功能– JEP 354 (3. Switch Expressions Enhancements – JEP 354)

Switch expressions were added as a preview feature in Java 12 release. It’s almost same in Java 13 except that the “break” has been replaced with “yield” to return a value from the case statement.

开关表达式已添加为Java 12版本中的预览功能。 在Java 13中几乎一样,只是“ break”已替换为“ yield”以从case语句返回值。

package com.journaldev.java13.examples;/*** JEP 354: Switch Expressions* https://openjdk.java.net/jeps/354* @author pankaj**/
public class SwitchEnhancements {@SuppressWarnings("preview")public static void main(String[] args) {int choice = 2;switch (choice) {case 1:System.out.println(choice);break;case 2:System.out.println(choice);break;case 3:System.out.println(choice);break;default:System.out.println("integer is greater than 3");}// from java 13 onwards - multi-label case statementsswitch (choice) {case 1, 2, 3:System.out.println(choice);break;default:System.out.println("integer is greater than 3");}// switch expressions, use yield to return, in Java 12 it was breakint x = switch (choice) {case 1, 2, 3:yield choice;default:yield -1;};System.out.println("x = " + x);}enum Day {SUN, MON, TUE};@SuppressWarnings("preview")public String getDay(Day d) {String day = switch (d) {case SUN -> "Sunday";case MON -> "Monday";case TUE -> "Tuesday";};return day;}
}

4.重新实现旧版套接字API – JEP 353 (4. Reimplement the Legacy Socket API – JEP 353)

The underlying implementation of the java.net.Socket and java.net.ServerSocket APIs have been rewritten. The new implementation, NioSocketImpl, is a drop-in replacement for PlainSocketImpl.

java.net.Socket和java.net.ServerSocket API的基础实现已被重写。 新的实现NioSocketImpl替代了PlainSocketImpl。

It uses java.util.concurrent locks rather than synchronized methods. If you want to use the legacy implementation, use the java option -Djdk.net.usePlainSocketImpl.

它使用java.util.concurrent锁而不是同步方法。 如果要使用旧版实现,请使用java选项-Djdk.net.usePlainSocketImpl

5.动态CDS存档– JEP 350 (5. Dynamic CDS Archive – JEP 350)

This JEP extends the class-data sharing feature, which was introduced in Java 10. Now, the creation of CDS archive and using it is much easier.

该JEP扩展了Java 10中引入的类数据共享功能。 现在,创建CDS存档并使用它要容易得多。

$ java -XX:ArchiveClassesAtExit=my_app_cds.jsa -cp my_app.jar$ java -XX:SharedArchiveFile=my_app_cds.jsa -cp my_app.jar

6. ZGC:取消提交未使用的内存– JEP 351 (6. ZGC: Uncommit Unused Memory – JEP 351)

This JEP has enhanced ZGC to return unused heap memory to the operating system. The Z Garbage Collector was introduced in Java 11. It adds a short pause time before the heap memory cleanup. But, the unused memory was not being returned to the operating system. This was a concern for devices with small memory footprint such as IoT and microchips. Now, it has been enhanced to return the unused memory to the operating system.

该JEP增强了ZGC,可以将未使用的堆内存返回给操作系统。 Z垃圾收集器是Java 11中引入的。 它会在堆内存清理之前增加短暂的暂停时间。 但是,未使用的内存没有返回给操作系统。 对于诸如IoT和微芯片等内存占用较小的设备,这是一个问题。 现在,它已得到增强,可以将未使用的内存返回给操作系统。

7. FileSystems.newFileSystem()方法 (7. FileSystems.newFileSystem() Method)

Three new methods have been added to the FileSystems class to make it easier to use file system providers, which treats the contents of a file as a file system.

已将三个新方法添加到FileSystems类中,以使其更易于使用文件系统提供程序,该系统将文件的内容视为文件系统。

  1. newFileSystem(Path)newFileSystem(路径)
  2. newFileSystem(Path, Map<String, ?>)newFileSystem(Path,Map <String,?>)
  3. newFileSystem(Path, Map<String, ?>, ClassLoader)newFileSystem(Path,Map <String,?>,ClassLoader)

8.具有命名空间支持的DOM和SAX工厂 (8. DOM and SAX Factories with Namespace Support)

There are new methods to instantiate DOM and SAX factories with Namespace support.

有支持命名空间的实例化DOM和SAX工厂的新方法。

  1. newDefaultNSInstance()newDefaultNSInstance()
  2. newNSInstance()newNSInstance()
  3. newNSInstance(String factoryClassName, ClassLoader classLoader)newNSInstance(字符串factoryClassName,ClassLoader classLoader)
//java 13 onwards
DocumentBuilder db = DocumentBuilderFactory.newDefaultNSInstance().newDocumentBuilder(); // before java 13
DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();

结论 (Conclusion)

It looks like that the 6-months release of Java has been working well. There are not many developer-specific features, but overall it’s a great release. It’s good to see the much-awaited text blocks string support.

Java的6个月发行版似乎运行良好。 没有很多特定于开发人员的功能,但是总体而言,这是一个不错的版本。 很高兴看到期待已久的文本块字符串支持。

参考资料 (References)

  • JDK 13 Release NotesJDK 13发行说明
  • OpenJDK 13 Download PageOpenJDK 13下载页面
  • Text Blocks Examples文本块示例
  • Eclipse.org Java 13 ExamplesEclipse.org Java 13示例

翻译自: https://www.journaldev.com/33204/java-13-features

java功能模块

java功能模块_Java 13功能相关推荐

  1. java功能模块_Java 14功能

    java功能模块 Keeping up with the six-month cycle tradition, after the release of Java 13 on September 17 ...

  2. java 常用模块_Java 常见面试题的模块

    Java 常见面试题的模块: Java 基础.容器.多线程.反射.对象拷贝.Java Web 模块.异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring Cl ...

  3. java使用:: 表达式_Java 13:切换表达式的增强功能

    java使用:: 表达式 您可能还记得我以前的文章,在Java 12中,传统的switch语句得到了增强,因此可以用作表达式. 在Java 13中,对该功能进行了进一步的更改 . break语句不能再 ...

  4. java 飞行记录器_Java 11功能– Java飞行记录器

    java 飞行记录器 在本文中,我们将看到如何利用Java Flight Recorder功能作为Java 11的一部分.之前,它是商业功能之一. 但是,对于带有JEP 328的 Java 11,它是 ...

  5. java日志模块_Java源码初探_logging日志模块实现

    一.用途 程序中记录日志,打印到控制台.文件等方式,记录过程可根据日志级别做筛选,日志格式可以自定义. 大概结构如下所示: 简要说明各个模块: (1) LogManager:管理LoggerConte ...

  6. python的功能模块_Python的功能模块[1] - struct - struct 在网络编程中的使用

    struct模块/ struct Module 在网络编程中,利用 socket 进行通信时,常常会用到 struct 模块,在网络通信中,大多数传递的数据以二进制流(binary data)存在.传 ...

  7. Odoo开源ERP:功能模块操作-销售功能篇

    客户基础资料 1. 所有的客户基础资料,智云ERP开账启用时,期初的客户数据如果大于200条,可以批量导入: 2. 点"销售/订单/客户"菜单可以查看.编辑修改.搜索所有的客户基础 ...

  8. 点赞功能模块-文章点赞功能实现

    PraiseController.java //点赞文章@RequestMapping(value = "on",method = RequestMethod.POST,consu ...

  9. CTO也糊涂的常用术语:功能模块、业务架构、用户需求、文档

    能模块.业务架构.需求分析.用户需求.系统分析.功能设计.详细设计.文档.业务.技术--很多被随口使用的名词,其实是含糊甚至错误的. 到底含糊在哪里,错误在哪里,不仅仅是新手软件开发人员糊涂,许多入行 ...

最新文章

  1. 如何设计一个本地缓存
  2. 设计模式-观察者模式(Observer)
  3. 标签的宽度_27 表格标签
  4. Android之AIDL服务
  5. 该伙伴事务管理器已经禁止了它对远程/网络事务的支持
  6. 计算机学院学生会宣传稿,计算机与信息工程学院学生会
  7. element中form表单resetFields()方法重置表单无效
  8. HTTP、HTTP2、HTTPS、SPDY等的理解及在spring-boot中的使用
  9. linux双显卡配置_Kali Linux 2.0 安装 NVIDIA显卡驱动实现双显卡(联想笔记本)
  10. mysql数据库索引使用总结和对比
  11. 将点分十进制转换为ip地址表示
  12. 傲腾内存简介 AEP 简介
  13. 浏览器播放语音SpeechSynthesisUtterance
  14. vscode :code runner运行include多个文件的cpp
  15. GoJS-FlowChart样例代码分析
  16. 线程池原理(ThreadPoolExecutor)
  17. hi3516dv300是几核处理器_HI3516DRBCV300-HI3516DRBCV300,hi3516DV300,HI3516-HI3516DRBCV300-香港科威芯电子有限公司...
  18. 【MySql】windows下重置数据库密码
  19. java 发送企业邮箱_java发送企业邮箱
  20. PLC实训 — 传感器介绍

热门文章

  1. POJ 1050 To the Max (最大子矩阵和)
  2. DevExpress GridControl双击获取行内容
  3. 利用Ajax实现DataGrid无刷新分页(AjaxGrid)【转】
  4. [转载] json字符串转list_Python入门进阶教程JSON操作
  5. [转载] python中string函数的用法_python中string模块各属性以及函数的用法
  6. Vue.js 学习笔记 八 v-for
  7. Ubuntu下添加打印机---之寻找设备lpinfo
  8. 论文学习: Journaling of Journal is (almost) Free 未整理
  9. 认识一个工具 Stopwatch
  10. Oracle使用NLSSORT函数实现汉字的排序