Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Alibaba senior technical expert Lei Juan: the language features of the post-Java 8 era that developers should pay attention to

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Author | Alibaba Senior Technical expert Thunder Scroll, GitHub ID @ linux-china

Introduction: at a time when Python, JavaScript and other programming languages are rising in vogue, the generation overlord Java is not as elegant as it was then, but it still sweeps the list of major programming languages, and is still the NO.1 in the major enterprise application development languages. Since Java 8, Java has introduced many useful new language features, as well as new tools and performance improvements. However, there are still many students who do not switch to the subsequent version of Java 8 in their daily development. This article will focus on the development direction and introduce you to the features of the post-Java 8 era.

First of all, we must admit that Java 8 is a landmark version, which I believe most Java programmers agree with, the most famous of which is Streams & Lambda, which makes Functional Programming possible and reinvigorates Java. This is also even though Oracle no longer supports the update of Java 8, various cloud vendors still actively support it, and the site is https://adoptopenjdk.net/, which allows Java 8 to remain for a very long time.

At present, many students' daily development has not been switched to the subsequent version of Java 8, so in this article, we intend to write a post-Java 8 feature, which is mainly biased towards development, not involving GC, Compiler, Java Module, Platform, etc., if you explain one by one, it is estimated that it will be a very long article, of course, you can write another article later. The following features affect our day-to-day coding.

Considering that Java 13 will be released soon, so the version covers from 9 to 13, while the way of Java Release is adjusted, some features are introduced in a certain version (preview), and a lot of enhancements and improvements have been made after receiving feedback, so it is not clear which version of the features are here, which you can understand as a hodgepodge of features after the Java 8 version. The resources come from the official Java feature introduction to each version of features and pluralsight.

Var keyword (local variable type derivation) Local-Variable Type Inference

Java supports generics, but if the type is very long and you are not particularly concerned, you can use the var keyword to make your code very concise. Java IDE supports var very well, so you don't have to worry about code hints.

Map store = new ConcurrentHashMap (); / / lambda BiFunction function1 = (var S1, var S2)-> S1 + S2; System.out.println (function1.apply (text1, text2))

Copy the confd file to the bin directory and start confd.

Sudo cp bin/confd / usr/local/binconfd

There are some minor limitations in actual use, such as null assignment problems, but these are not problems, so use them right away.

ProcessHandle

Although we rarely call system commands in Java, we do use them occasionally, all of which are ProcessBuilder. Another is the enhanced ProcessHandle, which allows you to learn some information about other processes, such as getting all processes, commands to start a process, startup time, and so on.

ProcessHandle ph = ProcessHandle.of (89810). Get (); System.out.println (ph.info ()); Collection factory methods

To create ArrayList, HashSet is still using the new method, which is a bit out of date, just use the factory method.

Set ints = Set.of (1,2,3); List strings = List.of ("first", "second"); new API of the String class

Here can not be listed one by one, to say a few important, after understanding, there is no need for a third-party StringUtils. Repeat, isEmpty, isBlank, strip, lines, indent, transform, trimIndent, formatted, etc.

HTTP 2 support

Of course, if you use OkHTTP 3, then there is no problem, if you do not want to introduce other development packages, then Java already supports HTTP 2, the code is basically the same, of course, both synchronous and asynchronous support.

HttpClient client = HttpClient.newHttpClient (); HttpRequest req = HttpRequest.newBuilder (URI.create ("https://httpbin.org/ip")). Header (" User-Agent "," Java ") .Get () .build (); HttpResponse resp = client.send (req, HttpResponse.BodyHandlers.ofString ()) System.out.println (resp.body ()); Text Block (JDK 13)

In the previous version, you had to have a large piece of text, and you had to convert the double quotation marks, which was very unsuitable for reading, as follows:

String jsonText = "{" id ": 1," nick ":" leijuan "}"

The new way text block:

/ / language=json String cleanJsonText = "" {"id": 1, "nick": "leijuan"} "

Much easier, you are free to write code, do not have to worry about all kinds of double quotation mark conversion, copy sharing conversion and so on. Wait a minute, why did you add / / language=json before cleanJsonText, what the heck is this? This is a feature of IntelliJ IDEA, your text block still has semantics, such as a HMTL, JSON, SQL, etc., after adding this, the code prompt immediately. Most people I don't tell him:)

Text block also has a small feature is the basic template feature support, you need to introduce some context variables in text block, directly% s, and then call the formatted method on it.

/ / language=html String textBlock = "" Hello% s ""; System.out.println (textBlock.formatted (nick)); Switch promotes Arrow Labels

Access the "- >" switch arrow, so you don't need to write so many break. The code is as follows:

/ / legacy switch (DayOfWeek.FRIDAY) {case MONDAY: {System.out.println (1); break;} case WEDNESDAY: {System.out.println (2); break;} default: {System.out.println ("Unknown") } / / Arrow labels switch (DayOfWeek.FRIDAY) {case MONDAY, FRIDAY, SUNDAY-> System.out.println (6); case TUESDAY-> System.out.println (7); case THURSDAY, SATURDAY-> System.out.println (8); case WEDNESDAY-> System.out.println (9);} Switch Expressions

That is, switch can have a return value, as shown in the code below:

/ / Yielding a value int i2 = switch (DayOfWeek.FRIDAY) {case MONDAY, FRIDAY, SUNDAY-> 6; case TUESDAY-> 7; case THURSDAY, SATURDAY-> 8; case WEDNESDAY-> 9; default-> {yield 10;}}

The keyword yield represents the return value of the switch expression.

I want to use these features right away

You said so much, it's all very good, but what's the use of our online or Java 8 environment? Just looking at it. Don't worry, someone thought of it, too. This project supports the transparent compilation of various grammars of JDK 12 + to the VM of Java 8, that is, there is no problem for you to run on Java 8 with these syntax features, so even if there is no problem in the Java 8 environment, the above features can be used.

How to use it? It's simple.

First download the latest JDK, such as JDK 13, and then add jabel-java-plugin to the dependency.

Com.github.bsideup.jabel jabel-javac-plugin 0.2.0

Then adjust the compiler plugin of maven and set source to the Java version you want, such as 13, target and release to 8. IntelliJ IDEA is automatically recognized and does not need to be adjusted.

Org.apache.maven.plugins maven-compiler-plugin 3.8.1 13 8 8

In this way you can happily use the features introduced.

Summary

If there are some features that have not been sorted out and are very useful, please give us some feedback, such as the adjustment of API, which is convenient for subsequent students to refer to.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report