• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 83
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic