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 expression
This 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 illustrates
The 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 types
Now you can omit the curly braces, the
return keyword, and the semicolon at the end
And finally you can rename the parameters to
h and
lAnd yes, you can still assign this lambda expression to a
Climb reference variable
And as you stated correctly, the
check() method takes in a
Climb object. So you could write something like this
But again, if you don't like to type so much, you can pass the lambda expression directly as the first parameter of the
check() method
And this still compiles successfully!
Let's have a look at the invocation of the
check() method from your code snippet
As you know by now, this can be rewritten as
So 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 this
Will 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 bookclarification 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).