• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lambda with 2 parameters for OCAJP 8 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand this situation and lambdas with 2 parameters.

Giving this code :

The method interface takes 2 int parameters height and limit so the lambda expression looks like this:
is something like :


How does the code climb.isTooHight(height, 10) compile if we don't have a value for height hence limit is set to 10?

The answer to this question is that it return "ok"
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not describing the test method which is in the Predicate functional interface. Here I think the Climb functional interface is used.
In this the lambda expression (h,l)->h>l is the method istooHight.
it returns true if height>limit. and you are passing the value of height to be 5. In the check method signature, second parameter is height.

In this expression if(climb.isTooHight(height, 10)), the second parameter to the isTooHight method is 10. If you look at the method signature of isTooHight, the second parameter is limit. and the first parameter height is 5, it is being got as the local variable height in the check method.

height is 5 and limit is 10 . so 5>10 returns false. so it happens like this.

If you do an SOP in the check method, you can find out.
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ramya.
Now I see that the lambda ((h,l)->h>l) is the first parameter of check method, and the other parameter is value of height which is set to 5.

That 5 confused me and I thought it was in the body of lambda expression. In fact it is the second parameter.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NP .. Lambdas can be very confusing at times
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:Thanks a lot Ramya.
Now I see that the lambda ((h,l)->h>l) is the first parameter of check method, and the other parameter is value of height which is set to 5.

That was indeed an excellent post with a great explanation. Have a cow, Ramya!

Claudiu Stroe wrote:That 5 confused me and I thought it was in the body of lambda expression. In fact it is the second parameter.

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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic