JAVA10 十大新特性

<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post">
                            <div class="htmledit_views">
                        
    <div class="rich_media_content" id="js_content">
                    


                    


                    
                    
                    <p style="margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;"><img style="border:2px solid rgb(238,238,238);visibility:visible !important;" src="https://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/TNUwKhV0JpQ9s9ib46onssFcYlORu0DeoRd9OmFRxuU5J7nIj5HP0o3TSHampvKsTGL8SfpJTd9ua2cJzTLouLQ/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1" alt="640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1"></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">Java 9才发布几个月,很多玩意都没整明白,现在Java 10又要来了。。</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">这时候我真尼玛想说:线上用的JDK 7 甚至JDK 6,JDK 8 还没用熟,JDK 9 才发布不久不知道啥玩意,JDK 10……</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">刚学Java的同学是不是感觉一脸蒙逼?!!!</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">就连我这个老司机也同样感觉如此!</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">Java 更新越来越快,我们做技术的也要跟上步伐,不然总会慢别人一拍,这新东西从国外到国内应用一般要好几年的时间,如果我们提前了解并应用这些新技术对自己不是坏事。</p><h3 style="color:rgb(21,153,87);line-height:1.35;font-size:20px;white-space:normal;"><a name="t0"></a>&nbsp;</h3><p><span style="color:rgb(248,122,122);font-size:20px;">Java 10的新特性</span></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">说了这么多,看Java 10都会有哪些特性来改变我们写代码的方式呢?!</p><p style="margin-right:1em;margin-left:1em;"><strong>1.局部变量类型推断</strong></p><p style="margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">局部变量类型推断可以说是Java 10中最值得注意的特性,这是Java语言开发人员为了简化Java应用程序的编写而采取的又一步,如下图所示。</span><br></p><p><br></p><p><img class="img_loading" src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/TNUwKhV0JpQ9s9ib46onssFcYlORu0DeoaibCM97HU6PJW9gIv4mevlIiaW50rS1IGOdKcmtrXHGjPeDVNr9j3Yfg/640?wx_fmt=png" alt="640?wx_fmt=png"></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">这个新功能将为Java增加一些语法糖 - 简化它并改善开发者体验。新的语法将减少与编写Java相关的冗长度,同时保持对静态类型安全性的承诺。</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">局部变量类型推断将引入"var"关键字,也就是你可以随意定义变量而不必指定变量的类型,如:</p><pre style="margin-top:0px;margin-bottom:0px;padding:8px 0px 6px;background-color:rgb(0,0,0);color:rgb(80,97,109);font-size:10px;line-height:12px;"></pre><ol class="list-paddingleft-2" style="list-style-type:none;"><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">List</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;</span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">String</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;">&gt; list = </span><span style="color:rgb(177,214,49);line-height:20px;font-size:13px !important;white-space: !important;">new</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> </span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">ArrayList</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;</span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">String</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;">&gt;(); </span></code></span></p></li><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">Stream</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;</span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">String</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;">&gt; stream = getStream();</span></code></span></p></li></ol><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">将被下面这个新语法所取代:</p><pre style="margin-top:0px;margin-bottom:0px;padding:8px 0px 6px;background-color:rgb(0,0,0);color:rgb(80,97,109);font-size:10px;line-height:12px;"></pre><ol class="list-paddingleft-2" style="list-style-type:none;"><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(177,214,49);line-height:20px;font-size:13px !important;white-space: !important;">var</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> list = </span><span style="color:rgb(177,214,49);line-height:20px;font-size:13px !important;white-space: !important;">new</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> </span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">ArrayList</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;</span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">String</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;">&gt;(); </span></code></span></p></li><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(177,214,49);line-height:20px;font-size:13px !important;white-space: !important;">var</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> stream = getStream();</span></code></span></p></li></ol><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">看完是不是有点JS的即视感???越来越像JS了吗?!虽然类型推断在Java中不是一个新概念,但在局部变量中确是很大的一个改进。</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">说到类型推断,从JDK 5引进泛型,到JDK 7的"&lt;&gt;"操作符允许不绑定类型而初始化List,再到JDK 8的Lambda表达式,再到现在JDK 10的局部变量类型推断,Java类型推断正大刀阔斧的向前发展。</p><pre style="margin-top:0px;margin-bottom:0px;padding:8px 0px 6px;background-color:rgb(0,0,0);color:rgb(80,97,109);font-size:10px;line-height:12px;"></pre><ol class="list-paddingleft-2" style="list-style-type:none;"><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(119,119,119);line-height:20px;font-size:13px !important;white-space: !important;">// 该运算符允许在没有绑定ArrayList &lt;&gt;的类型的情况下初始化列表</span></code></span></p></li><li><p><span style="color:rgb(74,74,74);display:block;line-height:22px;font-size:14px !important;"><code style="margin-left:-20px;line-height:12px;border-width:0px;font-size:10px;font-family:inherit !important;white-space:pre !important;"><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">List</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;</span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">String</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;">&gt; list = </span><span style="color:rgb(177,214,49);line-height:20px;font-size:13px !important;white-space: !important;">new</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> </span><span style="color:rgb(187,255,170);line-height:20px;font-size:13px !important;white-space: !important;">LinkedList</span><span style="color:rgb(238,238,238);line-height:20px;font-size:13px !important;white-space: !important;"> &lt;&gt;();</span></code></span></p></li></ol><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;"><strong style="color:rgb(0,0,0);">局部变量类型推荐仅限于如下使用场景:</strong></p><ul style="list-style-type:square;" class="list-paddingleft-2"><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">局部变量初始化</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">for循环内部索引变量</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">传统的for循环声明变量</span></p></li></ul><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;"><strong style="color:rgb(0,0,0);">Java官方表示,它不能用于以下几个地方:</strong></p><ul style="list-style-type:square;" class="list-paddingleft-2"><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">方法参数</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">构造函数参数</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">方法返回类型</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">字段</span></p></li><li><p><span style="color:rgb(74,74,74);line-height:22px;font-size:14px !important;">捕获表达式(或任何其他类型的变量声明)</span></p></li></ul><p style="text-align:center;"><br></p><p style="margin-right:1em;margin-left:1em;"><strong>2.GC改进和内存管理</strong></p><p><br></p><p><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">JDK 10中有2个JEP专门用于改进当前的垃圾收集元素。</span><br></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">第一个垃圾收集器接口是(JEP 304),它将引入一个纯净的垃圾收集器接口,以帮助改进不同垃圾收集器的源代码隔离。</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">预定用于Java 10的第二个JEP是针对G1的并行完全GC(JEP 307),其重点在于通过完全GC并行来改善G1最坏情况的等待时间。G1是Java 9中的默认GC,并且此JEP的目标是使G1平行。</p><p style="margin-right:1em;margin-left:1em;"><strong>3.线程本地握手(JEP 312)</strong></p><p style="margin-top:15px;text-align:left;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">JDK 10将引入一种在线程上执行回调的新方法,因此这将会很方便能停止单个线程而不是停止全部线程或者一个都不停。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>4.备用内存设备上的堆分配(JEP 316)</strong></p><p><br></p><p><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">允许HotSpot VM在备用内存设备上分配Java对象堆内存,该内存设备将由用户指定。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>5.其他Unicode语言 -&nbsp;标记扩展(JEP 314)</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;"></span><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">目标是增强java.util.Locale及其相关的API,以便实现语言标记语法的其他Unicode扩展(BCP 47)。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>6.基于Java的实验性JIT编译器</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">Oracle希望将其Java JIT编译器Graal用作Linux / x64平台上的实验性JIT编译器。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>7.根证书(JEP 319)</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">这个的目标是在Oracle的Java SE中开源根证书。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>8.根证书颁发认证(CA)</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">这将使OpenJDK对开发人员更具吸引力,它还旨在减少OpenJDK和Oracle JDK构建之间的差异。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>9.将JDK生态整合单个存储库(JEP 296)</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">此JEP的主要目标是执行一些内存管理,并将JDK生态的众多存储库组合到一个存储库中。</span></p><p style="margin-right:1em;margin-left:1em;"><strong>10.删除工具javah(JEP 313)</strong></p><p><br></p><p style="white-space:normal;border-width:0px;border-style:none;margin-top:15px;"><span style="color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;font-size:16px;white-space:pre-line;">从JDK中移除了javah工具,这个很简单并且很重要。</span></p><h3 style="font-size:20px;white-space:normal;color:rgb(21,153,87);line-height:1.35;"><a name="t1"></a>&nbsp;</h3><p><span style="color:rgb(248,122,122);">Java 10尝鲜</span></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">就像所有其他JDK版本一样,Oracle已经发布了一个Java 10初始候选版本,Java开发者可以下载此版本用来测试新功能。如果你对Java 10抱有期待想提早尝试,那么现在就开始吧!</p><blockquote style="margin-top:1em;margin-bottom:1.2em;padding:15px 15px 15px;color:rgb(129,145,152);border-left-width:6px;border-left-color:rgb(220,230,240);font-size:14px;line-height:22px;background:rgb(242,247,251);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;white-space:normal;"><p style="white-space:pre-line;line-height:30px;color:rgb(74,74,74);">尝鲜地址:http://openjdk.java.net/projects/jdk/10/</p></blockquote><h3 style="font-size:20px;white-space:normal;color:rgb(21,153,87);line-height:1.35;"><a name="t2"></a>&nbsp;</h3><p><span style="color:rgb(248,122,122);">最后的一点想法</span></p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">对于第一点新特性来说确实是一个大的跨越,其他特性或多或少都能带给开发者福音。但最终Java 10会是什么样,或许还会增加更多实用性的新功能或者优化,我们还得期待!</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">现在要做的就是,赶紧熟悉JDK 8,现在已经是很多互联网企业标配了。如果还在使用JDK4-7,那真的是要OUT了。。</p><p style="margin-top:15px;margin-bottom:15px;font-size:16px;white-space:pre-line;line-height:30px;color:rgb(74,74,74);font-family:Avenir, '-apple-system-font', '微软雅黑', sans-serif;">在不久的将来,Java 10将彻底改变你写代码的方式!</p><p style="min-height:1em;color:rgb(62,62,62);text-align:center;"><img class="img_loading" style="margin-right:auto;margin-left:auto;display:block;visibility:visible !important;" width="160" src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/fEsWkVrSk548p4UR0NNicbbfVFf3vqHLe8Aq1K2rqIsLxCTAYFuemO9Bu3rVXagXwtO3I9fXst3lEzr6G8sZelQ/640?wx_fmt=png" alt="640?wx_fmt=png"></p><p style="margin-top:10px;margin-bottom:5px;text-align:center;color:rgb(127,127,127);font-size:12px;letter-spacing:3px;line-height:25.5938px;"><span style="color:rgb(255,104,39);font-size:14px;">本文来自一Java大牛朋友的公众号,</span><span style="color:rgb(255,104,39);font-size:14px;">大家可以关注一下</span></p><img border="0" class="img_loading" style="visibility:visible !important;" title="" width="171" src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/TNUwKhV0JpT0nFKcvlC27s38To8SdpwxEBh1xXaGibE9zxUboZKtefZb4ZrcEib7BUBdOLKiceC48Hw40bC3VyuMA/640?wx_fmt=jpeg" alt="640?wx_fmt=jpeg"><p style="font-size:14px;">长按关注,更多精彩</p></div>
                  </div>
                    </div>

猜你喜欢

转载自blog.csdn.net/z19960415/article/details/80055935