Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 83
Hibernate Spring Java
  • 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?
 
Marshal
Posts: 70351
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very different.
Does Java8 even support function pointers?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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++.
 
author
Posts: 284
32
  • 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
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic