JDK13 features

JAVA13 overview

On September 17, 2019, the internationally renowned OpenJDK open source community released the latest version of the Java programming language environment, OpenJDK13.

Features: There are a total of 5 new JEPs (JDK Enhancement Proposals):
http://openjdk.java.net/projects/jdk/13/

Features:

350: Dynamic CDS Archives: Dynamic CDS Archives
351: ZGC: Uncommit Unused Memory: ZGC: Cancel the use of unused memory
353: Reimplement the Legacy Socket API: Reimplement the legacy socket API
354: Switch Expressions (Preview): switch expressions 355:Text Blocks ( Preview
): Text blocks

Grammatical level features

switch expression (preview)

Switch expressions were introduced as a preview feature in JDK 12. JDK 13 comes with a second preview of switch expressions. JEP 354 modified this feature by introducing the yield statement for returning values.

In the past, it was quite troublesome if we wanted to return content in switch. The general syntax was as follows:

String x = "3";
int i;
switch (x) {
    
    
	case "1":
		i=1;
		break;
	case "2":
		i=2;
		break;
	default:
		i = x.length();
		break;
}
System.out.println(i);

Use the following syntax in JDK13:

String x = "3";
int i = switch (x) {
    
    
	case "1" -> 1;
	case "2" -> 2;
	default -> {
    
    
		yield 3;
	}
};
System.out.println(i);

or:

String x = "3";
int i = switch (x) {
    
    
	case "1":
		yield 1;
	case "2":
		yield 2;
	default:
		yield 3;
};
System.out.println(i);

After that, there is an additional keyword in switch to jump out of the switch block, that is yield, which is used to return a value. The difference with return is that return will directly jump out of the current loop or method, while yield will only jump out of the current switch block.

Text block (preview)

In Java, it is usually necessary to use the String type to express strings in formats such as HTML, XML, SQL or JSON. When performing string assignment, escaping and concatenation operations are required before the code can be compiled. This expression is difficult to read and Difficult to maintain. A text block refers to a multi-line string, such as a formatted xml, json, etc. With text blocks, users don't need to escape, Java can do it automatically. Therefore, text blocks will improve the readability and writability of Java programs.

Define a piece of HTML code

<html>
	<body>
		<a href="http://www.xxx.com">xxx</a>
	</body>
</html>

Put this code into Java's String, and the following effect will appear:

String words =
    "<html>\n" +
	"\t<body>\n" +
	"\t\t<a href=\"http://www.xxx.com\">xxx</a></a>\n" +
	"\t</body>\n" +
	"</html>";

Spaces, newlines, indentations and special symbols are automatically escaped, but in JDK13 you can use this syntax:

String words = """
    <html>
        <body>
            <a href="http://www.mashibing.com">波波烤鸭</a>
        </body>
    </html>""";

Use """ as the start and end characters of the text block, and you can place multi-line strings in it without any escaping. It looks very refreshing.

Such as common SQL statements:

select empno,ename,sal,deptno
from emp
where deptno in (40,50,60)
order by deptno asc

Original way:

String query = "select empno,ename,sal,deptno\n" +
"from emp\n" +
"where deptno in (40,50,60)\n" +
"order by deptno asc";

Now way:

String newQuery = """
select empno,ename,sal,deptno
from emp
where deptno in (40,50,60)
order by deptno asc
""";

Below is a malformed block of text

String a = """"""; // 开始分隔符后没有行终止符
String b = """ """; // 开始分隔符后没有行终止符
String c = """
"; // 没有结束分隔符
String d = """
abc \ def
"""; // 含有未转义的反斜线(请参阅下面的转义处理)

At runtime, the text block will be instantiated as an instance of String, just like a string. A String instance derived from a text block is indistinguishable from an instance derived from a string. Two blocks of text with the same content will reference the same String instance, just like strings.

The compiler will remove extra spaces when compiling

In the following code, we .use it to represent the spaces in our code, and the spaces in these positions are redundant.

String html = """
..............<html>
.............. <body>
.............. <p>Hello, world</p>
.............. </body>
..............</html>
..............""";

These extra spaces are invisible to programmers, but they actually exist, so if the compiler does not process it, the contents of the two text blocks seen by the programmer may be the same, but this However, the presence of this extra space causes differences between the two text blocks, such as unequal hash values.

Notes on escaping

Use \n, \f and \r for vertical formatting of strings, and \b and \t for horizontal formatting. For example, the following code is legal:

String html = """
<html>\n
<body>\n
<p>Hello, world</p>\n
</body>\n
</html>\n
""";

Text blocks can be used anywhere that strings can be used . For example, text blocks and strings can be concatenated with each other:

String code = "public void print(Object o) {" +
"""
System.out.println(Objects.toString(o));
}
""";

API level features

Reimplementation of legacy socket API

The newly implemented NioSocketImpl replaces JDK1.0's PlainSocketImpl. This implementation shares the same internal infrastructure as the NIO implementation and is integrated with the existing buffer cache mechanism, so there is no need to use a thread stack. Apart from this, he has some other changes, such as using java.lang The .ref.Cleaner mechanism closes sockets, implements garbage collection on sockets that have not been closed, and handles timeout operations when the socket is in non-blocking mode during polling.

  • It is easy to maintain and debug, and uses the same JDK internal structure as NewI/O (NIO), so there is no need to use system native code.
  • It integrates with the existing buffer caching mechanism so that there is no need to use a thread stack for I/O.
  • It uses java.util.concurrent locks instead of synchronized synchronization methods to enhance concurrency capabilities.
  • The new implementation is the default implementation in Java 13, but the old implementation has not been removed. You can switch to the old version by setting the system property jdk.net.usePlainSocketImpl.

Other changes

ZGC cancels unused memory

In Java 13, JEP 351 once again enhanced ZGC to return unused heap memory to the operating system. ZGC currently cannot return memory to the operating system. Even memory that has not been used for a long time can only be accessed. This behavior is not friendly to any application or environment, especially those services that are sensitive to memory usage, such as:

  1. Pay-as-you-go container environment;
  2. An environment where applications may be idle for long periods of time and share and compete for resources with many other applications;
  3. Applications have very different heap space requirements during execution; for example, they may require more heap memory at startup than during stable operation.

Add obsolescence and removal

Add items

  • 添加FileSystems.newFileSystem(Path, Map<String, ?>) Method
  • 新的java.nio.ByteBuffer Bulk get/put Methods Transfer Bytes Without Regard to Buffer Position
  • Supports Unicode 12.1
  • Add -XX:SoftMaxHeapSize Flag, currently only works for ZGC
  • ZGC maximum heap size increased to 16TB

Remove item

  • Remove awt.toolkit System Property
  • Remove Runtime Trace Methods
  • Remove -XX:+AggressiveOpts
  • 移除Two Comodo Root CA Certificates、Two DocuSign Root CA Certificates
  • Remove the internal com.sun.net.ssl ​​package

Discarded items

  • Deprecated -Xverify:none and -noverify
  • rmic Tool is deprecated and ready for removal
  • javax.security.cert deprecated and prepared for removal

おすすめ

転載: blog.csdn.net/qq_28314431/article/details/132926168