• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Lambdas passed in to a method

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Doing some studying for the Java SE17 cert, and noticed this snippet of code:



Where I have put //HERE, I haven't seen a method being called with a lambda like this before...how is this compatible with Check(Climb climb, int height)? Normally I would expect to see an Object (typically a Climb one)..


Can someone explain how?
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just spotted this : https://coderanch.com/t/668480/certification/lambda-Chapter-pg-Java-OCA#3119904
which I will take a look, but any help here would be welcomed too. Thanks
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I get it now, so my question is...

Is this first parameter just returning null so the if statement sees its as false?

 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Bant wrote:I think I get it now, so my question is...

Is this first parameter just returning null so the if statement sees its as false?


When in doubt, extract it. A lambda is just a function/method with no name:

Your example shouldn't be able to compile unless I missed something, since values were never defined for x or y. If it does, it would probably be defaulting, so it would be the results of relativeCompare(0, 0);
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - it compiles, and I think you are right, it must be defaulting to 0 which is odd since its a var/local param...odd
 
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong with your check invoking. Check requires two arguments, a Climb and an int, and you supply these. Of course it compiles. But maybe I misundersttood something?
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Tim was referring to the values var x and var y, what values are they
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Bant wrote:I think Tim was referring to the values var x and var y, what values are they



If you don't assign a value to a numeric variable in Java, it will assume the default value of zero. That's a fundamental part of the language and was considered a vast improvement over older languages, which might simply leave whatever trash had been at that memory location previously. Zero might not be what you really wanted, but at least it was consistent and predictable, which greatly cuts down on random non-repeatable errors.
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see - isn't var deemed local though, so technically doesn't have a default value as local variables don't have a default value? I wonder if an exception was made here
 
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, I think Tim is sending you on a path to more confusion.  The original code does, indeed compile (well, once you replace '˃' with '>' - those are not the same character).  And the advice about "extracting" the lambda doesn't help unless you understand it well enough to know what the lambda means.  The confusion comes because the compiler is filling in the gaps for several things that are not explicitly specified here.

Note that when "(var x, var y)" is used, it's used inside the check() method:

So the compiler, to interpret the lambda, will use what it knows about the declared types of the check() method:

The only way the compiler can interpret the check() call is if the first argument is a Climb instance.  So the question is, can the compiler do that?  This involves a lot of behind the scenes "magic" to make that work, in ways that are very carefully spelled out in the JLS, but a real pain to understand all the details.  But basically, when the compiler sees a lambda, and it's in a place where the type is a functional interface, that is, an interface with just one abstract method, then the compiler will do whatever it can to interpret the lambda as an instance of that interface.  In this case, the interface is Climb:

Well, that's a functional interface, with just one (non-static, non-default) method.  So the compiler will do its best to interpret the lambda

as if it represents the missing method

That means that the two vars are interpreted as ints.  

It also needs to return a boolean - which we don't see happening, explicitly.  But here we can use another "magic" property of lambdas, that if they need to return something, and they end in a simple expression, they actually return that expression, without saying so.  So

is equivalent here to

and now it's considered as an implementation of the isTooHigh() method:

In the elder days of Java (1.1), the closest equivalent of this would have been an anonymous class:

(If you haven't learned about anonymous classes yet, just ignore for now.  They're much less important now than they used to be, though they're still legal and you do see them occasionally.)

But since Java 1.8, you can implement essentially the same thing much more concisely as

or even

(The var keyword wasn't adding anything useful there, anyway.)

As for this discussion of default values, I don't really know where that's coming from - I haven't tried to decode that branch of the conversation.  There are no default values for local variables or method parameters, only for member fields (static or non-static).  The only values that x and y ever have are the ones passed into it.  In the code shown, that's a height of 5 for x, and a constant 10 for y.  Defaults are not needed, which is good since they don't exist.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigh.  I keep finding things to fix in my post.  If you're replying, please re-read; I had some errors along the way...
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of additional rules for how lambda conversion works; I wasn't trying to get them all, but to give enough to understand how that lambda is interpreted as a Click, which is what the original question seemed to be.
 
See where your hand is? Not there. It's next to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic