Java functional programming

Functional programming provides an ability to pass a function (a method without encompassing class) as a parameter to a method. Java does it using the following trick:

– define a method method01(Interface01 inter) that accepts an interface Interface01 as a parameter;

– make sure that the interface Interface01 has only one abstract (not implemented) method. For example:

        interface Interface01 { 
            void method02(String name); 
        }

That’s it. Now you can pass the method method02() implementation into the method method01() without creating a class that implements the interface Interface01. Why? Because the compiler “looks” into the Interface01, “sees” only one method that has to be implemented, and “knows” that whatever code you are passing into the method01() as a parameter, it has to be code that implements the method02().

The Interface01 may have other already implemented methods, default or static, for example. Their presence does not change the fact that the interface has only one abstract method. Such an interface – with one abstract method only – is called a functional interface.

Or Interface01 will remain functional even if it inherits its only abstract method from the parent interface. For example, the following Interface02 is a functional interface too:

        interface Interface02 extends Interface01 {
            default void method03(String name){
                System.out.print("Name=");
                printName(name);
            }
            static void printName(String name){
                System.out.println(name);
            }
        }

Now let us look at an example. Here is a class that defines method01() as follows:

        class SomeClass {
            public void method01(Interface01 inter, String name){
                inter.method02(name);
            }
        }

Now let us invoke method01() in the following code excerpt: 

    SomeClass obj = new SomeClass();
    Interface01 inter = s -> System.out.println("Hello, " + s + "!");
    obj.method01(inter, "Nick");

If you run the above code, the result will be:

    Hello, Nick!

As you can see, we were able to write only one method implementation, without creating a class that implements Interface01 and its object. The format of the implementation is called a lambda expression. There is a series of articles that describes it and demonstrates its usage. It is very straightforward:

– parameters inside the parentheses “( )” (can be omitted for a single parameter, as in our example);

– an arrow token “->” that separates parameters declaration form the method body;

– the method body inside the braces “{ }” (can be omitted if the method consists only of one statement, as in our example).

The above example can be rewritten the following way:

    SomeClass obj = new SomeClass();
    Interface01 inter = 
        (String s) -> {System.out.println("Hello, " + s + "!");};
    obj.method01(inter, "Nick");

The result would be the same. We will talk about lambda expression syntax in another article. For now, let us finish the review of Java functional programming.

It is recommended adding to a functional interface the following annotation:

        @FunctionalInterface
        interface Interface01 {
           void method02(String name);
        }

Such an annotation informs the compiler about the programmer’s intent. So, the compiler can check if this interface contains only one abstract method or not. This way the error – having more than one abstract method – can be caught at the compile time.

As you can see, writing and using a functional interface is easy, but before doing it, check the package java.util.function. It defines many functional interfaces that can be used without creating your own one. You can read about these standard functional interfaces another time.

These are the basics of Java functional programming. It allows passing around method implementation as a function, which is especially helpful for a parallel processing and asynchronous processing in general. We will talk about it in more details later.

Read more detailed discussion of this topic in the following books:

, ,

Send your comments using the link Contact or in response to my newsletter.
If you do not receive the newsletter, subscribe via link Subscribe under Contact.

Powered by WordPress. Designed by Woo Themes