#Functional Interface vs Functions

10 messages · Page 1 of 1 (latest)

crisp abyss
#

I started learning about lambda and interfaces because I always thought was something Interesting even if at class they never showed me, so my question is...

Why would I use a functional Interface with one method use lambda when I can just make a class or make the method in Main?

balmy pewterBOT
#

This post has been reserved for your question.

Hey @crisp abyss! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

astral torrent
#

well, the issue is you can't

#

methods in java can't be referenced or passed around directly, they aren't first-class members

#

functional interfaces let you create objects (which are first-class members) that represent the method, so you can work around that limitation of the language

#

in practice, that's utilized in callbacks, ie functions that you provide to some other method, where the method can either call the function after doing something or with data, or use that function to integrate in some other task

#

lambdas are a syntax to implement functional interfaces; they're just syntactic sugar
before lambdas were introduced (in java 8 or 9, don't remember), anonymous classes implementing the respective interface were used (SAMs for Single Abstract Method, i believe? not sure on the terminology since it's not used much anymore)

crisp abyss
#

I understand, thanks a lot! Was really confused at first :)

balmy pewterBOT