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

Function pointers/delegates/callbacks  RSS feed

 
Andrzej Borucki
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have "Newton" class and I want pass functions f(x) and f'(x) pass to object of class Newton. How do it in Java?
 
Campbell Ritchie
Marshal
Posts: 55682
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot pass function pointers or anything like that in Java® ...


but you can create a functional interface and instantiate it from a λ
Start by going through the code for a well‑known functional interface from the Java standard installation; there is a file called src.zip inside your instillation folder. Unxip it and go java→util→concurrent→Callable.java and you should find the code for this. Read the actual code rather than the comments. Now you know the sort of thing you want to write in your .java file.Now you can pass that λ to a method taking a Foo parameter and Bob's your uncle

I suggest you search for tutorials about λs. You should find something in the Java™ Tutorials.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use DoubleUnaryOperator for that.

An example:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!