从Java8到Java18:不同版本间有哪些新特征?

从Java8到Java18:不同版本间有哪些新特征?Java8在Java开发世界里,无疑为当前使用占比最多的Java版本,虽然新版本已经更新到18版。Java 7 – LTS, 2026年。

大家好,欢迎来到IT知识分享网。

从Java8到Java18:不同版本间有哪些新特征?

拨开云雾见天日

Java8在Java开发世界里,无疑为当前使用占比最多的Java版本,虽然新版本已经更新到18版。也许哪天项目需要从Java8升级到Java更新的版本,可能性更大的,也许哪天要开始一个新项目,也许不会考虑Java8,而是考虑当前最新且技术支持时间最长的版本。这里我们就看看不同版本间有哪些新特征,做个预习。

在Java众多版本中,会有一些版本,是长期支持版本(Long Term Support, LTS),也就是说,这些版本的更新更多是关注稳定性,安全性及性能,而不是添加新的功能。并且这些版本维护时间也长,比如Java8会被维护到2030年。以下是Java LTS版本,及至少维护到的年限。

  • Java 7 – LTS, 2026年
  • Java 8 – LTS,2030年
  • Java 11 – LTS, 2026年
  • Java 17 – LTS,2029年
  • 所有其他Java版本 – 非LTS

那现在看看从Java 8到Java 18的新特性列表, Java 18的最近更新是在2022年3月, 支持日期到2022年9月。

New Java language features since Java 8

Feature

Since

Preview since

Simple Web Server (a.k.a com.sun.net.httpserver.SimpleFileServer)
Java API: SimpleFileServer.createFileServer(new InetSocketAddress(9000), path, logLevel).start();
Cmd Line: jwebserver -p 9000

18

Code Snippets in Java API Documentation
{@snippet : lines of code} instead of <pre>{@code lines of code }</pre>

18

UTF-8 by Default – instead of the charset defined by the system.
Run javac -encoding UTF-8 with JDK 8-17 to check for Java source encoding issues.

18

Context-Specific Deserialization Filters – allow applications to configure deserialization filters, either specify a pattern via a system property, or a filter factory via a system property or via ObjectInputFilter.Config

17

Pattern Matching for switch – like instanceof for switch; switch is an expression and can be assigned
String result = switch (o) {
case null -> null;
case 0 -> throw new RuntimeException(“Cannot be 0”); // Special cases
case Integer i && i > 0 -> “Positive number”;
case Integer i -> “Negative number”; // 0 and positive numbers handled by above rules
case String s -> s;
case Point p -> p.toString();
case int[] ia -> “Array length” + ia.length;
default -> “Something else”;
}

17

Foreign Function & Memory API (an alternative to JNI)

16

Vector API – express computations that compile to optimal hardware instructions

16

Sealed classes (inheritance only for allowed classes):
public abstract sealed class Shape permits Circle, Rectangle, Square {…}

17

15

Record type – data classes with implicit getters, constructor, equals, hashCode and toString methods:
record Point(int x, int y) { }

16

14

Static members in inner classes (part of Record type JEP)
new Object() {
static record MyData(String data) {};
public static final int CONSTANT = 1;
};

16

16

Pattern Matching for instanceof:
if (x instanceOf String s) { String a = s; }

16

14

Text blocks:
String query = “””
SELECT “EMP_ID”, “LAST_NAME”
FROM “EMPLOYEE_TB”;
“””

15

13

New String methods (formatted, stripIndent, translateEscapes)

15

Switch expressions:
boolean isWeekend = switch (day) {
case SATURDAY, SUNDAY -> true;
default -> false;
};

14

12

New String methods (indent, transform)

12

CompactNumberFormat class

12

New String methods (repeat, isBlank, strip, lines)

11

var type allowed in Lambda Parameters:
(@NonNull var x) -> process(x)

11

New HTTP client API

11

9

TLS v1.3 – support for a new SSL/TLS protocol version

11

var type allowed for local variables:
var length = str.length();

10

Flow API (reactive streams)

9

Java Platform Module System (modules)

9

Collection factory methods:
List.of(a, b, c); Set.of(d, e, f, g); Map.of(k1, v1, k2, v2);

9

Stream API improvements (takeWhile, dropWhile, ofNullable, iteratewith condition)

9

Multi-Resolution Image API

9

Stack-Walking API

9

this.getClass().getPackageName()

9

Process API updates (detailed info about processes, e.g. ID, onExit, destroy)

9

new methods in CompletableFuture API (delay, timeout)

9

Interface private methods

9

since and forRemoval in @Deprecated

9

Interface Default and Static Methods

8

Method References

8

Optional class

8

Lambda expressions

8

Functional interfaces

8

Stream API

8

Effectively Final Variables

8

Repeating Annotations

8

New Date Time API

8

New JDK tools and features since OpenJDK 8

Tool / feature

Since

Experimental since

Packaging Tool

14

Epsilon (no-op) GC

11

Experimental Java-Based JIT Compiler (Graal VM)

10

Shenandoah GC

15

12

Z GC (JEP 377)

15

11

Helpful NullPointerExceptions

14

Launching Java files as scripts

11

Flight recorder (data collection framework for troubleshooting)

11

Docker Container Support:
-XX:-UseContainerSupport

10,
8u191

Flexible heap size selection:
-XX:MaxRAMPercentage

10,
8u191

Application Class-Data Sharing (CDS)

10

jlink – custom JRE image, subset of JRE

9

JShell (Java REPL) – run Java commands interactively

9

Multi-Release JAR Files

9

Compact Strings

9

Deprecated/removed features and APIs:

Feature / API

Deprecated since

Removed since

Deprecate Finalization for Removal (finalize() and similar methods)

18

Deprecate the Security Manager for Removal

17

Deprecate the Applet API for Removal

17

Constructors of primitive wrapper classes (e.g. new Integer(1))

16

RMI Activation

15

17

ParallelScavenge + SerialOld GC Combination

14

Strongly Encapsulate JDK Internals (except sun.misc.Unsafe and some more)
– internal JDK classes won’t be available anymore

9

17

Nashorn JavaScript Engine

11

15

Solaris and SPARC Ports

14

15

CMS GC

9

14

Pack200 Tools and API

11

14

Java FX (moved to OpenJFX)

11

Java EE and CORBA modules (JAX-WS, JAXB, JAF, Common Annotations, CORBA, JTA)
Look here to find replacements for the removed modules

9

11

javah Native-Header Generator

10

Applet API (Deprecated For Removal in Java 17)

9

jhat Heap Visualizer

9

Launch-Time JRE Version Selection

9

rt.jar and tools.jar

9

The extension mechanism (java.ext.dirs property)

9

Rarely-Used GC Combinations

8

9

参考资料:

  • .Lost in Coding – NEW FEATURES BETWEEN JAVA 8 AND JAVA 18
  • Java Magazin – The art of long-term support and what LTS means for the Java ecosystem
  • Oracle Java SE Support Roadmap
    https://www.oracle.com/java/technologies/java-se-support-roadmap.html

#教育听我说#

#职场升值季#

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/58092.html

(0)
上一篇 2024-07-07 16:33
下一篇 2024-07-07 18:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信