gpt4 book ai didi

java - 关于 Java lambda 相等性和/或实例化

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:38 25 4
gpt4 key购买 nike

<分区>

为什么下面的代码片段在第二次通过时打印为真?它不应该是一个新实例吗?

import java.util.function.Supplier;

public class Foo {
public static void main(String[] args) throws Exception {
Supplier<Long> old = () -> System.nanoTime();

for (int i = 0; i < 3; i++) {
/* false true true
Supplier<Long> foo = System::nanoTime;*/

Supplier<Long> foo = () -> System.nanoTime();

/* false false false
Supplier<Long> foo = new Supplier<Long>() {
@Override
public Long get() {
return System.nanoTime();
}
};
//*/

System.out.printf("%s %s %s%n", foo == old, foo, old);

old = foo;
}
}
}

false Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$1/1534030866@133314b
true Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$2/122883338@1ddc4ec2
true Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$2/122883338@1ddc4ec2

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com