Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java 8 support functions as first class objects?

I've read today about the Java 8 release. But I don't understand fully the concept of reference methods in Java 8. Does this mean that Java now has the support of functions as first class objects? I have seen, how to construct a reference to function. But it seems to me, that the Converter object they provide has quite limited functionality. Is it now possible in Java:

  • to pass the function as the argument to another function?
  • to return the function as the return value from another function?
  • and what about closures? Are they implemented fully like in functional languages, or they do have some limitations? It seems to me that there are some limitations (you cannot change the value of the variable you reference in a closure, it must be marked as final and etc).
like image 338
SPIRiT_1984 Avatar asked Mar 20 '14 10:03

SPIRiT_1984


People also ask

Are functions first-class objects in Java?

Functions as First Class Objects In the functional programming paradigm, functions are first class objects in the language. That means that you can create an "instance" of a function, as have a variable reference that function instance, just like a reference to a String, Map or any other object.

Are functions first-class objects?

In Python, functions behave like any other object, such as an int or a list. That means that you can use functions as arguments to other functions, store functions as dictionary values, or return a function from another function.

Can you manipulate functions as first-class objects?

To say that functions are first-class in a certain programming language means that they can be passed around and manipulated similarly to how you would pass around and manipulate other kinds of objects (like integers or strings). You can assign a function to a variable, pass it as an argument to another function, etc.


2 Answers

No, not first class functions. Lambda expression are wrapped in a interface, and some syntatic sugar applied for brevity.

But you can;t create a function on its own and pass it around different methods, whether thats a key point or not is a different question.

like image 165
NimChimpsky Avatar answered Oct 16 '22 08:10

NimChimpsky


It is possible. How do you do it?

First construct a "Functional Interface" (or use one of the provided ones). A functional interface is an interface with a single method. java.lang.Runnable is an example.

Second, write a method that takes a functional interface as a parameter.

public void doAThing(Runnable r) {
    r.run();
}

Third, write a method with the correct signature.

public class MyClass {
    public void runAThing() {
        System.out.println("I executed!");
    }
}

Fourth, call the function passing in a method reference.

MyClass mc = new MyClass();
doAThing(mc::runAThing);

You'll note that none of the classes you've wrote ever explicitly implements Runnable. This is handled for you by the compiler.

You can do something similar using a lamdba expression:

doAThing(() -> System.out.println("I executed as a lamdba expression!"));

To return the function as a value from another function, just return an instance of Runnable.

like image 23
Sean Reilly Avatar answered Oct 16 '22 08:10

Sean Reilly