java-feature-after8.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com, 2023/12/22">
  9. <title>java 8之后新特性</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>java 8之后新特性</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="author3" class="author">2023/12/22</span><br>
  36. </div>
  37. <div id="toc" class="toc2">
  38. <div id="toctitle">Table of Contents</div>
  39. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  40. <li><a href="#_关键特性">1. 关键特性</a></li>
  41. <li><a href="#_switch优化">2. switch优化</a></li>
  42. <li><a href="#_try_with_resources增强">3. try-with-resources增强</a></li>
  43. <li><a href="#_新增api">4. 新增API</a>
  44. <ul class="sectlevel2">
  45. <li><a href="#_集合增强">4.1. 集合增强</a></li>
  46. <li><a href="#_字符串增强">4.2. 字符串增强</a></li>
  47. <li><a href="#_数学运算增强">4.3. 数学运算增强</a></li>
  48. <li><a href="#_httpclientjdk_11">4.4. HttpClient(<code>JDK 11</code>)</a></li>
  49. <li><a href="#_hexformatjdk_17">4.5. HexFormat(<code>JDK 17</code>)</a></li>
  50. <li><a href="#_arrays">4.6. Arrays</a></li>
  51. <li><a href="#_unixdomainsocketaddressjdk_16">4.7. UnixDomainSocketAddress(<code>JDK 16</code>)</a></li>
  52. <li><a href="#_reactive_streams_publish_subscribe_frameworkjdk_9">4.8. Reactive Streams publish-subscribe framework(<code>JDK 9</code>)</a></li>
  53. <li><a href="#_varhandlejdk_9">4.9. VarHandle(<code>JDK 9</code>)</a></li>
  54. </ul>
  55. </li>
  56. <li><a href="#_杂项">5. 杂项</a></li>
  57. <li><a href="#_preview">6. Preview</a>
  58. <ul class="sectlevel2">
  59. <li><a href="#_string_templatesjdk_21_preview">6.1. String Templates(<code>JDK 21 Preview</code>)</a></li>
  60. <li><a href="#_unnamed_variablesjdk_21_preview">6.2. Unnamed Variables(<code>JDK 21 Preview</code>)</a></li>
  61. <li><a href="#_unnamed_classes_and_instance_main_methodsjdk_21_preview">6.3. Unnamed Classes and Instance Main Methods(<code>JDK 21 Preview</code>)</a></li>
  62. </ul>
  63. </li>
  64. </ul>
  65. </div>
  66. </div>
  67. <div id="content">
  68. <div id="preamble">
  69. <div class="sectionbody">
  70. <div class="paragraph">
  71. <p>Java 21是最近的LTS版本,在今年10月份发布,包含了多个令人兴奋的新特性如虚拟线程等,本文用于总结Java9-21的重要特性,帮助还停留在Java 8的开发者迅速了解新版本并完成迁移</p>
  72. </div>
  73. </div>
  74. </div>
  75. <div class="sect1">
  76. <h2 id="_关键特性"><a class="anchor" href="#_关键特性"></a>1. 关键特性</h2>
  77. <div class="sectionbody">
  78. <div class="paragraph">
  79. <p>Java架构师Brian Goetz写了如下几篇文章用于介绍关键新特性</p>
  80. </div>
  81. <div class="hdlist">
  82. <table>
  83. <tr>
  84. <td class="hdlist1">
  85. 局部变量的类型推断
  86. </td>
  87. <td class="hdlist2">
  88. <p><a href="https://www.infoq.cn/article/ET9ffWtGxErjvwexCl5G">Java 特性聚焦:局部变量的类型推断</a></p>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td class="hdlist1">
  93. 封闭类
  94. </td>
  95. <td class="hdlist2">
  96. <p><a href="https://www.infoq.cn/article/x66h5q2sPwSLsmbxkA1T">Java 新特性前瞻:封印类</a></p>
  97. </td>
  98. </tr>
  99. <tr>
  100. <td class="hdlist1">
  101. 记录类
  102. </td>
  103. <td class="hdlist2">
  104. <p><a href="https://www.infoq.cn/article/bJrVTPcuXGG0cHQpSzae">Java 14 特性专题报道:记录</a></p>
  105. </td>
  106. </tr>
  107. <tr>
  108. <td class="hdlist1">
  109. 文本块
  110. </td>
  111. <td class="hdlist2">
  112. <p><a href="https://www.infoq.cn/article/L53YgjdXISy793CfLjSI">Java 特性专题报道:文本块</a></p>
  113. </td>
  114. </tr>
  115. <tr>
  116. <td class="hdlist1">
  117. 模式匹配
  118. </td>
  119. <td class="hdlist2">
  120. <p><a href="https://www.infoq.cn/article/1CB7jiLBmvU3Hbxw2ZO7">Java 新特性解析:模式匹配</a></p>
  121. </td>
  122. </tr>
  123. <tr>
  124. <td class="hdlist1">
  125. 面向数据编程
  126. </td>
  127. <td class="hdlist2">
  128. <p><a href="https://www.infoq.cn/article/e0cVtyDv3LwKAvJ3076N">Java 中的面向数据编程</a></p>
  129. </td>
  130. </tr>
  131. <tr>
  132. <td class="hdlist1">
  133. 虚拟线程
  134. </td>
  135. <td class="hdlist2">
  136. <p><a href="https://www.infoq.cn/article/YaBqqD7fd6kX97GbhkGm">虚拟线程:大规模 Java 应用的新基石</a></p>
  137. </td>
  138. </tr>
  139. </table>
  140. </div>
  141. </div>
  142. </div>
  143. <div class="sect1">
  144. <h2 id="_switch优化"><a class="anchor" href="#_switch优化"></a>2. switch优化</h2>
  145. <div class="sectionbody">
  146. <div class="paragraph">
  147. <p>switch 内部现在支持使用表达式,表达式的结果作为switch的返回值,使用表达式时不需要像之前那样添加break语句</p>
  148. </div>
  149. <div class="listingblock">
  150. <div class="content">
  151. <pre class="highlight"><code class="language-java" data-lang="java">int numLetters = switch (day) {
  152. case MONDAY, FRIDAY, SUNDAY -&gt; 6;
  153. case TUESDAY -&gt; 7;
  154. default -&gt; {
  155. String s = day.toString();
  156. int result = s.length();
  157. yield result;
  158. }
  159. };
  160. </code></pre>
  161. </div>
  162. </div>
  163. </div>
  164. </div>
  165. <div class="sect1">
  166. <h2 id="_try_with_resources增强"><a class="anchor" href="#_try_with_resources增强"></a>3. try-with-resources增强</h2>
  167. <div class="sectionbody">
  168. <div class="paragraph">
  169. <p>try-with-resources支持声明多个需要释放的资源</p>
  170. </div>
  171. <div class="listingblock">
  172. <div class="content">
  173. <pre class="highlight"><code class="language-java" data-lang="java">try (BufferedReader br1 = new BufferedReader(...);
  174. BufferedReader br2 = new BufferedReader(...)) {
  175. System.out.println(br1.readLine() + br2.readLine());
  176. }
  177. </code></pre>
  178. </div>
  179. </div>
  180. <div class="paragraph">
  181. <p>可以明显感觉到这样的代码可读性很差,Java 9中对此进行了增强如下,注意此时br1和br2尽管没有显式声明为final但已经有了final的语义,不允许再修改</p>
  182. </div>
  183. <div class="listingblock">
  184. <div class="content">
  185. <pre class="highlight"><code class="language-java" data-lang="java">BufferedReader br1 = new BufferedReader(...);
  186. BufferedReader br2 = new BufferedReader(...);
  187. try (br1; br2) {
  188. System.out.println(br1.readLine() + br2.readLine());
  189. }
  190. </code></pre>
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. <div class="sect1">
  196. <h2 id="_新增api"><a class="anchor" href="#_新增api"></a>4. 新增API</h2>
  197. <div class="sectionbody">
  198. <div class="sect2">
  199. <h3 id="_集合增强"><a class="anchor" href="#_集合增强"></a>4.1. 集合增强</h3>
  200. <div class="paragraph">
  201. <p>Java 9新增了多个方法用于简化集合创建</p>
  202. </div>
  203. <div class="listingblock">
  204. <div class="content">
  205. <pre class="highlight"><code class="language-java" data-lang="java">Set&lt;Integer&gt; mySet = Set.of(1, 2, 3);
  206. List&lt;Integer&gt; myList = List.of(1, 2, 3);
  207. Map&lt;String, Integer&gt; myMap = Map.of("one", 1, "two", 2);
  208. </code></pre>
  209. </div>
  210. </div>
  211. <div class="paragraph">
  212. <p>Java 21中新增了三个有序集合接口</p>
  213. </div>
  214. <div class="ulist">
  215. <ul>
  216. <li>
  217. <p>SequencedCollection</p>
  218. </li>
  219. <li>
  220. <p>SequencedSet</p>
  221. </li>
  222. <li>
  223. <p>SequencedMap</p>
  224. </li>
  225. </ul>
  226. </div>
  227. <div class="imageblock">
  228. <div class="content">
  229. <img src="images/sequencedcollectiondiagram20220216.jpg" alt="sequencedcollectiondiagram20220216">
  230. </div>
  231. </div>
  232. </div>
  233. <div class="sect2">
  234. <h3 id="_字符串增强"><a class="anchor" href="#_字符串增强"></a>4.2. 字符串增强</h3>
  235. <div class="ulist">
  236. <ul>
  237. <li>
  238. <p><code>String.splitWithDelimiters(String regex, int limit)</code> 用于分隔字符串,返回的数组包含匹配到的分隔符</p>
  239. </li>
  240. <li>
  241. <p><code>StringBuffer/StringBuilder.repeat(CharSequence cs, int count)</code> 追加多个相同的字符串序列到末尾</p>
  242. </li>
  243. <li>
  244. <p><code>String.repeat(int count)</code> 返回当前字符串重复count次后的字符串</p>
  245. </li>
  246. <li>
  247. <p><code>String.lines()</code> 返回字符串中所有行的流</p>
  248. </li>
  249. <li>
  250. <p><code>String.transform(Function&lt;? super String, ? extends R&gt; f)</code> 字符串转换为R</p>
  251. </li>
  252. </ul>
  253. </div>
  254. </div>
  255. <div class="sect2">
  256. <h3 id="_数学运算增强"><a class="anchor" href="#_数学运算增强"></a>4.3. 数学运算增强</h3>
  257. <div class="paragraph">
  258. <p>为了使Java语言能够应用于机器学习等大规模数据计算场景中,新版本的Java增强了数学运算的API</p>
  259. </div>
  260. <div class="ulist">
  261. <ul>
  262. <li>
  263. <p><code>Math.clamp(long value, int min, int max)</code> 用于得到三个值的中间值</p>
  264. </li>
  265. <li>
  266. <p><code>Math.divideExact()</code> 用于long和int的除法,结果溢出时抛出异常</p>
  267. </li>
  268. <li>
  269. <p><code>BigInteger.parallelMultiply()</code> 更高性能的两个大数相乘</p>
  270. </li>
  271. </ul>
  272. </div>
  273. </div>
  274. <div class="sect2">
  275. <h3 id="_httpclientjdk_11"><a class="anchor" href="#_httpclientjdk_11"></a>4.4. HttpClient(<code>JDK 11</code>)</h3>
  276. <div class="listingblock">
  277. <div class="content">
  278. <pre class="highlight"><code class="language-java" data-lang="java">HttpClient httpClient = HttpClient.newBuilder().build();
  279. HttpRequest request =
  280. HttpRequest.newBuilder()
  281. .uri(URI.create("https://advancedweb.hu/"))
  282. .GET()
  283. .build();
  284. HttpResponse&lt;String&gt; response =
  285. httpClient.send(request, BodyHandlers.ofString());
  286. </code></pre>
  287. </div>
  288. </div>
  289. </div>
  290. <div class="sect2">
  291. <h3 id="_hexformatjdk_17"><a class="anchor" href="#_hexformatjdk_17"></a>4.5. HexFormat(<code>JDK 17</code>)</h3>
  292. <div class="listingblock">
  293. <div class="content">
  294. <pre class="highlight"><code class="language-java" data-lang="java">HexFormat.of().toHexDigits(123);
  295. // ==&gt; "0000007b"
  296. HexFormat.of().fromHexDigits("0000007b");
  297. // ==&gt; 123
  298. </code></pre>
  299. </div>
  300. </div>
  301. </div>
  302. <div class="sect2">
  303. <h3 id="_arrays"><a class="anchor" href="#_arrays"></a>4.6. Arrays</h3>
  304. <div class="ulist">
  305. <ul>
  306. <li>
  307. <p><code>mismatch()</code> 方法返回两个数组第一个不相同元素的索引</p>
  308. </li>
  309. </ul>
  310. </div>
  311. </div>
  312. <div class="sect2">
  313. <h3 id="_unixdomainsocketaddressjdk_16"><a class="anchor" href="#_unixdomainsocketaddressjdk_16"></a>4.7. UnixDomainSocketAddress(<code>JDK 16</code>)</h3>
  314. <div class="paragraph">
  315. <p>用于实现Unix domain socket的SocketAddress实现类</p>
  316. </div>
  317. </div>
  318. <div class="sect2">
  319. <h3 id="_reactive_streams_publish_subscribe_frameworkjdk_9"><a class="anchor" href="#_reactive_streams_publish_subscribe_frameworkjdk_9"></a>4.8. Reactive Streams publish-subscribe framework(<code>JDK 9</code>)</h3>
  320. </div>
  321. <div class="sect2">
  322. <h3 id="_varhandlejdk_9"><a class="anchor" href="#_varhandlejdk_9"></a>4.9. VarHandle(<code>JDK 9</code>)</h3>
  323. <div class="paragraph">
  324. <p>VarHandle API to replace the field and array related operations of java.util.concurrent.atomic and sun.misc.Unsafe in order to and provide low-level access mechamisms, e.g. atomic write.</p>
  325. </div>
  326. </div>
  327. </div>
  328. </div>
  329. <div class="sect1">
  330. <h2 id="_杂项"><a class="anchor" href="#_杂项"></a>5. 杂项</h2>
  331. <div class="sectionbody">
  332. <div class="ulist">
  333. <ul>
  334. <li>
  335. <p>jwebserver启动一个简单的web服务,一般用于开发测试</p>
  336. </li>
  337. <li>
  338. <p>移除Nashorn</p>
  339. </li>
  340. <li>
  341. <p>NullPointerException优化,提示导致空指针的变量</p>
  342. </li>
  343. </ul>
  344. </div>
  345. </div>
  346. </div>
  347. <div class="sect1">
  348. <h2 id="_preview"><a class="anchor" href="#_preview"></a>6. Preview</h2>
  349. <div class="sectionbody">
  350. <div class="sect2">
  351. <h3 id="_string_templatesjdk_21_preview"><a class="anchor" href="#_string_templatesjdk_21_preview"></a>6.1. String Templates(<code>JDK 21 Preview</code>)</h3>
  352. <div class="paragraph">
  353. <p>变量与字符串连接为一个新的字符串是代码非常频繁的操作,目前版本的Java可以使用加号连接、
  354. StringBuilder、String.format来实现,但这些都不如字符串模板来的简单直观</p>
  355. </div>
  356. <div class="listingblock">
  357. <div class="content">
  358. <pre class="highlight"><code class="language-java" data-lang="java">var name = "Duke";
  359. var info = STR."My name is \{name}";
  360. </code></pre>
  361. </div>
  362. </div>
  363. </div>
  364. <div class="sect2">
  365. <h3 id="_unnamed_variablesjdk_21_preview"><a class="anchor" href="#_unnamed_variablesjdk_21_preview"></a>6.2. Unnamed Variables(<code>JDK 21 Preview</code>)</h3>
  366. <div class="paragraph">
  367. <p>对于不会实际使用的变量可以把变量名或者变量类型与变量名声明为下划线</p>
  368. </div>
  369. <div class="listingblock">
  370. <div class="content">
  371. <pre class="highlight"><code class="language-java" data-lang="java">var _ = mySet.add(x); // ignore the return value
  372. try {
  373. // ...
  374. } catch (Exception _) { // ignore the exception object
  375. // ...
  376. }
  377. list.stream()
  378. .map((_) -&gt; /* ... */) // ignore the parameter
  379. .toList();
  380. if (r instanceof Point(int x, _)) {
  381. // work with x, ignore second parameter
  382. }
  383. </code></pre>
  384. </div>
  385. </div>
  386. </div>
  387. <div class="sect2">
  388. <h3 id="_unnamed_classes_and_instance_main_methodsjdk_21_preview"><a class="anchor" href="#_unnamed_classes_and_instance_main_methodsjdk_21_preview"></a>6.3. Unnamed Classes and Instance Main Methods(<code>JDK 21 Preview</code>)</h3>
  389. <div class="listingblock">
  390. <div class="content">
  391. <pre class="highlight"><code class="language-java" data-lang="java">// This is a complete and runnable program!
  392. void main() {
  393. System.out.println("Hello, World!");
  394. }
  395. </code></pre>
  396. </div>
  397. </div>
  398. </div>
  399. </div>
  400. </div>
  401. </div>
  402. <div id="footer">
  403. <div id="footer-text">
  404. Last updated 2024-03-18 05:44:42 UTC
  405. </div>
  406. </div>
  407. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  408. <script>
  409. if (!hljs.initHighlighting.called) {
  410. hljs.initHighlighting.called = true
  411. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  412. }
  413. </script>
  414. <script src="https://utteranc.es/client.js"
  415. repo="pxzxj/articles"
  416. issue-term="title"
  417. label="utteranc"
  418. theme="github-light"
  419. crossorigin="anonymous"
  420. async>
  421. </script>
  422. </div>
  423. </div>
  424. </div>
  425. </body>
  426. </html>