Not without declaring the type as well. Since Java is a statically-typed language, and functions are not first class citizens, the compiler needs to know what type your lambda is. A function can't just be free-floating, it always needs to be associated with either a class or an instance of a class.
Runnable r = () -> {
System.out.println("Hello world!");
};
r.run();
But: You can cast the lambda to the Runnable
type, and give the compiler a hint as to what kind of @FunctionalInterface
you're implementing:
((Runnable)() -> {
System.out.println("Hello world!");
}).run();
Or without the braces, which makes it a one-liner:
((Runnable)() -> System.out.println("Hello world!")).run();
I imagine that's about as close as you'll get!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…