Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is lambda expression and functional pointers different or same? How?  RSS feed

 
jagrutkumar dalwadi
Ranch Hand
Posts: 83
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure that lambda expression and function pointers are same or different. can anyone please help me in this?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very different.
Does Java8 even support function pointers?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 8 will not have function pointers.

There is a notation, however, to refer to methods by their name (method references). It uses the "::" notation. The following pieces of code are equivalent:

You can assign method references to functional interfaces (interfaces with a single abstract method):

So, it looks a little bit like a function pointer in languages such as C or C++.
 
Cay Horstmann
author
Ranch Hand
Posts: 195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper says, the equivalent of a function pointer in C/C++ is the method reference in Java.

For example,



does what you think it does. But lambda expressions are more powerful. Look at Kernighan & Ritchie's example for function pointers:



Here, strcmp is the equivalent of String::compare. And numcmp is a function that they wrote elsewhere. But in C, you must write it as a separate function. With lambda expressions, you can write



and then just pass it to Arrays.sort.

Cheers,

Cay
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!