我有以下代码试图使用 Optional 类:
import java.util.Optional;
// one class needs to have a main() method
public class HelloWorld
{
public String orelesMethod() {
return "hello";
}
public void test() {
String value;
value = Optional.ofNullable(null).orElse(orelesMethod());
System.out.println(value);
}
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
HelloWorld hello = new HelloWorld();
hello.test();
}
}
编译时,它说:
incompatible types: Object cannot be converted to String
value = Optional.ofNullable(null).orElse(orelesMethod());
我找不到它是什么问题,有人可以帮忙吗?
心有法竹
aluckdog
沧海一幻觉
相关分类