() -> System.out.println(" running...")
The colon is missing.() -> System.out.println(" running...")
It must colon at the end of statement or curly brackets.
Am I correct?
Moderator: admin
The colon is missing.() -> System.out.println(" running...")
Without { and } you can call not only "void" method.Further, since it does not return anything, the body part must be such that it does not return anything either. Thus, you can either use a method call that returns void or some code enclosed within { and } that does not return anything.
Code: Select all
public static void main(String[] args) {
run(() -> voidMethod());
run(() -> intMethod());
}
public static void run(Runner x) {
x.run();
}
public static void voidMethod() {
System.out.println("voidMethod");
}
public static int intMethod() {
System.out.println("intMethod");
return 0;
}
Code: Select all
interface Runner {
public void run();
}
interface Runner2 {
public int run();
}
public class TestClass {
public static void main(String[] args) {
run(() -> voidMethod());
run(() -> intMethod());
}
public static void run(Runner x) {
System.out.println("In runner");
x.run();
}
public static void run(Runner2 x) {
System.out.println("In runner2");
x.run();
}
public static void voidMethod() {
System.out.println("voidMethod");
}
public static int intMethod() {
System.out.println("intMethod");
return 0;
}
}
Code: Select all
public static void voidMethod() {
System.out.println("voidMethod");
}
public static int intMethod() {
System.out.println("intMethod");
return 0;
}
It is equivalent to:ale8989 wrote:I'm sorry, how can
the expression () -> intMethod() bind to run(Runner x) in first place? It has a return value...Code: Select all
public static void voidMethod() { System.out.println("voidMethod"); } public static int intMethod() { System.out.println("intMethod"); return 0; }
Good question. You need to think at a completely different level while working with lambdas. The first thing that you should remember is that lambdas are a compile time thing. When you compile the code, the compiler converts lambda expressions into anonymous inner class code. There is no trace of a lambda expression in the generated class file.ale8989 wrote: Inside TestClass we call Runner run() insiede run(Runner x), so my Big question:
Why is possibile to refere to a "Runner" reference, when calling intMethod() (inside the lambda) which returns an int... and not a "Runner"?
But how can I pass an integer to a method that wants a reference of an interface? [/u]
Code: Select all
new Runner() {
public void run(){
TestClass.intMethod();
}
}
Dear Admin,
Code: Select all
System.out.println("blabla")
Code: Select all
class X
void someMethod(Runner r){
}
}
Hey there,admin wrote: ↑Tue Jan 12, 2021 9:22 amI am not sure I understand your question correctly. But the question is just looking for a valid lambda expression that can be supplied, for example, to a method that expects a Runner object. Something like:To call it, you would do: new X.someMethod(() -> System.out.println("running...")); The part inside the ( ) is the lambda expression that captures Runner interface. As you can see, no semicolon should be present after () -> System.out.println("running...").Code: Select all
class X void someMethod(Runner r){ } }
System.out.println("running...") is not a statement in this case. It is an expression. You might want to read about the difference between a statement and an expression from Section 6.1.2 of Deshmukh's OCP Java 11 Part 1 Fundamentals if you have any confusion.
Code: Select all
interface Runner {
public void run();
}
class X{
void someMethod(Runner r){
r.run();
}
}
public class TestClass{
public static void main(String[] args) {
new X().someMethod(() -> System.out.println("running..."));
}
}
https://docs.oracle.com/javase/tutorial ... sions.htmlA return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:
email -> System.out.println(email)
Users browsing this forum: Google [Bot] and 96 guests