Java语言特性系列
本文主要讲述一下Java10的新特性
重磅
)
相关解读: java10系列(二)Local-Variable Type Inference重磅
)
相关解读: Java10来了,来看看它一同发布的全新JIT编译器上面列出的是大方面的特性,除此之外还有一些api的更新及废弃,主要见What’s New in JDK 10 - New Features and Enhancements,这里举几个例子。
/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/src.zip!/java.base/java/util/Optional.java
输出
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.orElseThrow(Optional.java:371)
at com.example.FeatureTest.testOrElseThrow(FeatureTest.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
java9新增的of工厂方法的接口参数是一个个元素,java10新增List.copyOf, Set.copyOf,及Map.copyOf用来从已有集合创建ImmutableCollections
Collectors新增了toUnmodifiableList, toUnmodifiableSet,以及 toUnmodifiableMap方法
@Test(expected = UnsupportedOperationException.class)
public void testCollectionCopyOf(){
List<String> list = IntStream.rangeClosed(1,10)
.mapToObj(i -> "num"+i)
.collect(Collectors.toUnmodifiableList());
list.add("not allowed");
}
java10最主要的新特性,在语法层面就属于Local-Variable Type Inference,而在jvm方面307: Parallel Full GC for G1以及317: Experimental Java-Based JIT Compiler都比较重磅,值得深入研究。
官方解读
)官方细项解读
)