记录是自 Java 14(第一个预览版)和 Java 15(第二个预览版)以来的新语言功能。根据我的理解,它们将用于减少不可变数据对象中的样板代码。
所以这一行:
public record Person (String firstName, String lastName) {}
Run Code Online (Sandbox Code Playgroud)
相当于声明一个具有私有final字段、每个字段的getter、一个公共构造函数以及equals、hashCode和toString方法的类。
@Value然而,这与使用 lombok注释几乎相同:
@Value
public class Person {
String firstName;
String lastName;
}
Run Code Online (Sandbox Code Playgroud)
除了你显然不需要处理 lombok 依赖之外,使用记录还有什么优点吗?
我有以下密封接口(Java 15):
public sealed interface Animal permits Cat, Duck {
String makeSound();
}
Run Code Online (Sandbox Code Playgroud)
该接口由 2 个类实现:
public final class Cat implements Animal {
@Override
public String makeSound() {
return "miau";
}
}
public non-sealed class Duck implements Animal {
@Override
public String makeSound() {
return "quack";
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我之间的差异final和non-sealed?final阻止我创建其他子类,但什么行为non-sealed适用于Duck?
由于 Ed25519 出现的时间不长(在 JDK 中),因此关于如何使用它的资源很少。
虽然他们的示例非常简洁和有用,但我在理解密钥解析方面做错了什么时遇到了一些困难。
他们的公钥是从 iDevice 发送的数据包中读取的。
(这么说吧,它是一个字节数组)
通过搜索并尽力了解密钥的编码方式,我偶然发现了这条消息。
4. The public key A is the encoding of the point [s]B. First,
encode the y-coordinate (in the range 0 <= y < p) as a little-
endian string of 32 octets. The most significant bit of the
final octet is always zero. To form the encoding of the point
[s]B, copy the least significant bit of the x coordinate to the
most significant bit of the final octet. …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行一个 scala 程序,其中 Java 16 出现错误。我的同事正在使用 Java 15,一切都很好。当我在终端中输入时java -version,它说我正在使用 Java 15。但是,当我运行时sbt run -v,它说它正在使用 Java 16,因此程序会抛出错误。
我看到人们谈论这个sbt-extra东西,但没有太多关于如何使用它的解释。我的 Mac 上什至没有安装 Java 16,所以我真的很困惑为什么 SBT 会这么说。
Oracle 最近发布了 JDK 15,我想知道 NetBeans IDE 的最新版本 Apache NetBeans 12.1 是否支持 JDK 15。我知道 NetBeans 不正式支持 JDK 15,但我问是否有人知道它非正式地支持它。谢谢!
我正在使用 Java 15 的新记录功能,以及它如何与反射交互。我遇到了一些奇怪的行为,有时我可以通过反射访问记录的构造函数,有时则不能。例如,给定以下 Java 文件:
Recording.java:
public class Recording {
public static void main(String[] args) {
System.out.println("Constructors: " + MainRecord.class.getConstructors().length);
System.out.println("Methods: " + MainRecord.class.getDeclaredMethods().length);
}
record MainRecord(int i, String s) {}
}
Run Code Online (Sandbox Code Playgroud)
其行为如下:
? javac --enable-preview --release 15 Recording.java
Note: Recording.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
? java --enable-preview Recording
Constructors: 0
Methods: 5
Run Code Online (Sandbox Code Playgroud)
换句话说,对 的调用getConstructors()没有找到任何构造函数(而对 getDeclaredMethods() 的调用确实找到了方法)。我不明白为什么不,因为构造函数确实存在:
? javap Recording\$MainRecord
Compiled from "Recording.java"
final class Recording$MainRecord extends java.lang.Record {
Recording$MainRecord(int, …Run Code Online (Sandbox Code Playgroud) 刚刚在 Java 15 中遇到了一个新特性,即“文本块”。我可以假设一个变量可以通过与“+”运算符连接来添加到文本块中,如下所示:
String html = """
<html>
<body>
<p>Hello, """+strA+"""</p>
</body>
</html>
""";
Run Code Online (Sandbox Code Playgroud)
但是他们是否提供了任何方式,以便我们可以以在许多其他语言中流行的方式添加变量,如下所示:
String html = """
<html>
<body>
<p>Hello, ${strA}</p>
</body>
</html>
""";
Run Code Online (Sandbox Code Playgroud)
这个问题可能听起来很愚蠢,但在某些情况下可能很有用。
Java 15引入了(非预览)文本块功能。它允许通过从行中剥离公共空白前缀来定义多行字符串文字而不会破坏代码缩进。JEP 378 中描述了该算法。
但是,在使用制表符和空格混合缩进的情况下,“公共空白前缀”究竟是如何定义的?
例如,以下情况下的字符串值是什么(·表示空格,? 表示制表符):
? ? ····字符串文本 = """ ? ? ····?第一行 ? ··········?线2 ? ····?? """;
使用 OpenJDK 进行的简单测试显示结果字符串为:
第一行 ··?线2
所以看起来 Javac 只是计算空白符号,包括空格和制表符,并使用计数——平等对待空格 (0x20) 和制表符 (0x09)。这是预期的行为吗?
旁注:这不是一个纯粹的理论问题;它对于具有混合空格/制表符缩进和大型代码库的项目具有实际重要性。
我在我的代码中使用 Java 15 预览功能记录,并将记录定义如下
public record ProductViewModel
(
String id,
String name,
String description,
float price
) {
}
Run Code Online (Sandbox Code Playgroud)
在控制器级别我有以下代码
@Put(uri = "/{id}")
public Maybe<HttpResponse> Update(ProductViewModel model, String id) {
LOG.info(String.format("Controller --> Updating the specified product"));
return iProductManager.Update(id, model).flatMap(item -> {
if(item == null)
return Maybe.just(HttpResponse.notFound());
else
return Maybe.just(HttpResponse.accepted());
});
}
Run Code Online (Sandbox Code Playgroud)
模型中的 UI没有传递id的值,但是,它作为路由参数传递。现在我想在控制器级别设置值,比如
model.setid(id) // Old style
Run Code Online (Sandbox Code Playgroud)
如何将值设置为记录特定属性
我正在使用 runAsync() 运行数百个函数。所有的函数都会修改一些静态可用的列表,因此不需要返回任何内容。在继续我的处理之前,我想确保它们都完成了。这是适当的等待方式吗?有没有更简单的方法来完成我想要做的事情?
List<CompletableFuture<Void>> futures = new ArrayList<>();
active_sites.forEach(site -> site.getEntryPoints().stream().map(EntryPoint::scanEntryPoint).forEach(futures::add));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
Run Code Online (Sandbox Code Playgroud) java ×10
java-15 ×10
string ×2
ed25519 ×1
java-record ×1
kotlin ×1
lombok ×1
netbeans ×1
reflection ×1
sbt ×1
scala ×1
sealed-class ×1