About Question enthuware.ocajp.i.v8.2.1475 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ale8989
Posts: 5
Joined: Fri Nov 17, 2017 9:12 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by ale8989 »

Hello Paul,
sorry for the delay.
Now this makes absolutely sense!
Thank you very much, perfect explanation!

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by Denyo1986 »

admin wrote:
Wed Apr 08, 2015 11:34 am
No, it is just a lambda expression. It is not a statement.
Dear Admin,

I dont understand your response to this question.
An expression would be 5 == 10 or object.isArray() or something like this. But here we print something text on the console, which imho clearly is a statement (therefore we always write a ; after it).
All articles about Lambda expressions say that an expression does not need semicolon, one or more statement do need {} and ;

I dont understand why in this example here the part

Code: Select all

System.out.println("blabla") 
is not a statement that needs to accompanied by a ";".
Could you please elaborate on this?


Then, on a second note, I never understood this whole question in the first place. The question says which lambda expression captures the above interface. Nowhere in mentioned interface is anything printed out to the console. And nowhere in the lambda is any reference done to the runner interface. So, for me, they have hardly anything to do with each other, except for the fact that both dont take parameters and dont return anything (the latter is even not entirely true).

So how are we actually capturing that interface? Is capture the right word here?

Or are we only looking at similarities / common grounds between a functional interface and a lambda expression that have nothing to do with each other?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by admin »

I 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:

Code: Select all

class X
  void someMethod(Runner r){
  }
}
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...").


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.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by Denyo1986 »

admin wrote:
Tue Jan 12, 2021 9:22 am
I 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:

Code: Select all

class X
  void someMethod(Runner r){
  }
}
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...").


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.
Hey there,

thanks for the explanation but a link to an offline book is really a easy-to-use ressource. My exam is this Friday, therefore no chance to read a whole book just to get one question answered (even though I wish I could).
Do you have any other (easily available) sources that will support your statement?

Cause when I research, I only find results that support my view (an "expression" calculates to a value, which println() certainly doesnt, a statement "does something" which println() clearly does. Furthermore, every article about lambdas says that an expression can be without ";" but a statement must have the semicolon.
How can it be explained that here, in this case, a statement does not need a semicolon (or that the definition of a statement changes compared to te rest of the java world).

I am quite confused. Hope you can help me to get my head around this. Thanks for your time and effort.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by admin »

Ok, I think I understand your question now. You are looking at System.out.println("running...") in isolation. But the question is about the "lambda expression". So, yes, if you just look at System.out.println("running..."), then it is not an expression because it doesn't return a value. But "() -> System.out.println("running...")" (without the quotes) is a valid lambda expression and it doesn't need semicolon.

You can actually try out the following code, which proves that it doesn't need semicolon:

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...")); 
   }
}
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by Denyo1986 »

Thanks a lot again.
Got it now.

This is the rule that I was missing:
A 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)
https://docs.oracle.com/javase/tutorial ... sions.html

Because I read everywhere that the body of the lambda expression must consist of one expression (without ";") or one or several statements that need to have a ";" each. However, the exception from that rule that is cited above explains why for this statement (i.e. a call of the void method println() ) does not need the ";". When I was assessing the options of the question I was looking mostly for correct syntax, that is why I stumbled upon that.

Maybe you could add this info in the explanation that void method calls dont need ";" since I was not the first one to got confused by this.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1475 :

Post by admin »

Good point. Added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 87 guests