一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第7天,点击查看活动详情。
Java8于2014年3月18日发布,截止到2022年4月6日,当前最新发行版本是Java18。版本17、11和8是目前支持的长期支持(LTS)版本。这篇文章带领大家回顾从Java 8 开始每个版本的特性,小板凳坐好,发车了!想看上一篇文章,点击这儿面试反杀 | 请谈谈Java8-18引入的新特性(一)
Java 9 新特性
Stream.ofNullable,从 Java 8开始,我们可以使用 Stream.of (Object... values)创建一个元素流,从 Java 9中我们可以对单个元素使用 Stream.ofNullable 来创建一个流或返回一个空流。
List<Location> locList = new ArrayList<>(Arrays.asList(loc, loc1, loc2));
Stream.ofNullable(locList);
复制代码
Private Methods in Interface
Java 8在 Interface 中引入了默认方法的概念。如果有多个默认方法希望共享相同的逻辑,那么可以使用私有方法实现公共逻辑。
Java Modules
Java模块是一种技术,包和它们的相关资源可以被组合在一起。一个模块包含一个模块描述符,它将指定模块名称、依赖模块、其他模块可用的包,等等。
最终目标之一是在运行时减小应用程序的大小。如果我们不使用 GUI,我们就不需要在我们的应用程序中添加这个模块作为依赖项。
当然还有其它的新特性,包括但不限于:
- Try-With Resources
- Anonymous Classes
- @SafeVarargs Annotation
- Collection Factory Methods
- New Version-String Scheme
- JShell: The Java Shell (REPL)
- Installer Enhancement for Microsoft windows and many more
Java 10 新特性
local variable type inference
从 Java 10开始,我们可以初始化非空的局部变量并将它们赋值给 var 类型。变量的类型是由上下文决定的。
例子如下:
public class Sample {
public static void main(String[] args) {
var x = 10;
var y = 11;
System.out.println(x + y);
var obj = new TryVar();
System.out.println(obj.getName());
var c; //Illegal, Compiler will throw an error -> "Cannot use 'var' on variable without initializer"
}
}
class TryVar {
String name;
public String getName() {
return "my name";
}
}
复制代码
copyOf()
java.util.list,java.util.map和java.util.set每个都有一个新的静态方法copyof(集合)。
它返回给定集合的未经可执行的副本:
@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
List<Integer> copyList = List.copyOf(someIntList);
copyList.add(4);
}
复制代码
任何修改此类集合的尝试都会导致Java.Lang.UnsupportedOperationExceptionRuntime异常。
当然还有其它的新特性,包括但不限于:
- Performance Improvements
- toUnmodifiable*()
- Unmodifiable Collections
- Container Awareness
Java 11 新特性
Java 11作为LTS版本
New String Methods
Java 11在 String 类中添加了一些新的方法: isBlank、 lines、 strip、 stripLeading、 stripTrailing 和 repeat。
从多行字符串中提取非空白的字符串:
String multilineString = "Baeldung helps \n \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");
复制代码
New File Methods
现在从文件读写字符串更加容易了,们可以使用 Files 类中新的 readString 和 writeString 静态方法。
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");
复制代码
Collection to an Array
Collection 接口包含一个新的缺省 toArray 方法,该方法接受一个 IntFunction 参数。这使得从集合中创建正确类型的数组变得更加容易:
List sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
assertThat(sampleArray).containsExactly("Java", "Kotlin");
复制代码
当然还有其它的新特性,包括但不限于:
- Running Java File with single command
- Local-Variable Syntax for Lambda Parameters
- Nested Based Access Control
- JEP 321: HTTP Client
- JEP 328: Flight Recorder
未完待续,下面继续讲各个版本的新特性,敬请期待!