• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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"
 
Ranch Hand
Posts: 182
18
  • 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 R Subramanian
Ranch Hand
Posts: 182
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NP .. Lambdas can be very confusing at times
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
     
    Don't get me started about those stupid light bulbs.
      Bookmark Topic Watch Topic
    • New Topic