• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about lambda in Chapter 4 Question 27 pg 230 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Tami Wakana
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused on the explanation of the answer
The code is



There is a compiler error on line 7 and the explanation states that it is because h is a StringBuilder and the interface method takes in two ints.
I understand that, but the check method takes in Climb climb, not an int. Can someone clarify this further for me?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tami Wakana,

First of all, a warm welcome to CodeRanch!

Tami Wakana wrote:There is a compiler error on line 7 and the explanation states that it is because h is a StringBuilder and the interface method takes in two ints.
I understand that, but the check method takes in Climb climb, not an int. Can someone clarify this further for me?

Of course! A lambda expression can be very tricky and it is very easy to get confused, certainly if you are unfamiliar with the syntax.

If you want to create a lambda expression, you need a functional interface. In this example, the Climb interface is the functional interface. It has one method (isTooHigh) with two int parameters height and limit returning a boolean value. Giving this information you can create the following lambda expressionThis lambda expression takes two int parameters height and limit (which are the same parameters as the method in the functional interface Climb) and returns a boolean: true if height is greater than limit (which is the same return type as the method in the functional interface Climb). So you can assign this lambda expression to a Climb reference variable as this code snippet illustratesThe compiler is happy with it, because the parameters and return type are exactly the same as the method in the functional interface Climb.

If you don't like to type so much, you can get rid of some (optional) things (because the compiler can deduct this information from the method in the functional interface Climb. Let's start with the parameter typesNow you can omit the curly braces, the return keyword, and the semicolon at the endAnd finally you can rename the parameters to h and lAnd yes, you can still assign this lambda expression to a Climb reference variableAnd as you stated correctly, the check() method takes in a Climb object. So you could write something like thisBut again, if you don't like to type so much, you can pass the lambda expression directly as the first parameter of the check() methodAnd this still compiles successfully!

Let's have a look at the invocation of the check() method from your code snippetAs you know by now, this can be rewritten asSo when the compiler tries to compile this, he knows that the type of both parameters h and l are int (because it's the same as the parameters of the method in the functional interface Climb) and on an int primitive you can't invoke the append() method as it does not exist on a primitive. You need a StringBuilder reference variable to be able to invoke this method.

Now it's time for a (free) pop quiz question! What do you think will be the output of the code snippet if we add the parameter types to the lambda expression on line7, like thisWill the Climber class compile or not (and why)?

If you want to know and learn more about predicates, lambdas and functional interfaces, you should definitely have a look at these topics:
  • Predicates in Lambda (this is probably the one where Ramya learned everything about lambdas and was able to provide such a great explanation
  • lambdas in Boyarsky and Selikoff book
  • clarification in chapter 5 predicates in page 214 (Java OCA 8 Programmer I Study Guide, Sybex)
  • could we say 'out' is inner class of 'System' class ?


  • Hope it helps!
    Kind regards,
    Roel

    PS. I had to add an additional toString() call, because the isEmpty() method doesn't exist in the StringBuilder class. And without this additional call, the answer to my pop quiz question would be very easy In my opinion that's probably a minor errata item (although it doesn't affect the correct answer for this question at all).
     
    Tami Wakana
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Roel De Nijs,

    Thank you for replying!! It clarified a lot! The lambda is just another way to write a Climb object I guess, and I didn't catch that at all

    I think for your pop quiz, it shouldn't compile because the interface takes in and 2 ints, not a StringBuilder and an int! Now I understand where the explanation is coming from

    Thanks so much!
     
    Roel De Nijs
    Sheriff
    Posts: 10666
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tami Wakana wrote:I think for your pop quiz, it shouldn't compile because the interface takes in and 2 ints, not a StringBuilder and an int! Now I understand where the explanation is coming from

    You are absolutely spot-on! Well done
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic