The concept of Lambda expressions and Functional Interface(FI) is itself very large in scope. However, for the OCA Java 8, you are expected to know only the basics of Lambda Expression.
Today, we’ll cover lambda expressions in great details for your Java OCA exam.
To understand why to even use lambda expressions , you can refer this post. It also gives you a gist of a predefined Functional Interface usage – Predicate<T>
defined in java.util.function
package.
For now, let’s get started.
A functional interface is a Java interface with only one non-Object class abstract method. The abstract method defined within a functional interface is referred to as functional method. Remember below points regarding functional interfaces :
java.lang.Object
class in a FI.static
and default
methods.@FunctionalInterface
. If annotated, the code will fail to compile if we make an attempt to have more than one non-Object class abstract method in a FI.java.util.function
package.Yes, you got it right ! Based on the definition of Functional Interfaces, existing interfaces like Runnable
, Comparable
etc are also Functional Interfaces.
//compiles fine- equals is from Object class @FunctionalInterface interface User{ boolean test(User user); boolean equals(Object obj); } //compiles fine - any number of static and default methods are allowed @FunctionalInterface interface User{ boolean test(User user); default void sayHello(){System.out.println("Hello");} static void sayBye(){System.out.println("Bye");} } //Fails to compile - more than one abstract method(non-Object) @FunctionalInterface interface User{ boolean test(User user); void sayHello(); }
Lambda expressions implement functional interfaces by defining anonymous functions which can be passed as an argument to some method.
A lambda expression is of the form :
lambda_parameter_list -> lambda_body
There are some interesting rules you need to know when writing a lambda expression.Let’s have a look!
(x,y,z) -> x+y+z;
//inferred type parameters (x,y,z) -> x+y+z; //declared type parameter (int x,int y, int z) -> x+y+z;
//x and y are integers based on the definition of Predicate<Integer> Predicate<Integer> p = (x,y) -> x+y;
//valid declaration, x is single-inferred type x -> System.out.println(x); //valid declaration, s is single-declared type (String s) -> System.out.println(s); String s -> System.out.println(s); // invalid
x,y -> x+y; //is invalid (x,y) -> x+y; //is valid (String x, String y) -> x+y; //valid
final
is only allowed for declared-type parameters.
//valid for declared type (final int a,int b) -> a+b; //invalid for inferred type (final x, y) -> x+y; //fails to compile
//Examples of single-expression body a -> System.out.println(a); (a,b) -> a+b; //Examples of block of statements as lambda body a -> { System.out.println(a);}; a -> { System.out.println(a); System.out.println(10*a);};
//no return type - void a -> System.out.println(a); a -> null; //compiles fine, returns null //returns a value (a,b) -> a+b;
(a,b) -> return a+b; //fails to compile (a,b) -> a+b; //This expression itself means it returns a+b as an output (a,b) -> {return a+b;}; //compiles fine
a -> a++; //non-void return type a -> a/=2; //non-void return type a -> System.out.println(a); // void return type
//function type : String -> void interface DummyFI{ void sayHello(String txt); } /**The function type of target type and type of *lambda expression are compatible *in this example **/ //type of lambda expression : String -> void :: compatible DummyFI ex1 = s -> System.out.println(txt); /**The function type of target type and type of *lambda expression are not compatible *in this example and the code will fail to compile **/ //type of lambda expression : String -> String :: not - compatible DummyFI ex1 = s -> { return "Bye";};
/* Below code will compile fine * the return value will be simply ignored. */ DummyFI obj = s -> new String("Hello");
I know this blog post is having a lot of information to digest all at once. But I assure you, going through it a couple of times will make it all clear and things will make sense.
The knowledge gained through this blog post will not only help in scoring well on your Java OCA exam but will also help you professionally as a Java Developer.
I also intend to discuss with you the scope of the lambda expressions but it’s better to cover that topic in a separate post. So, Stay tuned !
Also, don’t forget to checkout other related Java OCA Exam preparation articles on the blog.
Hi! There is a mistake in the point 6, as “Hello” is not a statement expressions and it will fail. However this works: `DummyFI obj = s -> new String(“Hello”);`
Explained here: https://stackoverflow.com/questions/29262002/why-does-this-java-8-lambda-fail-to-compile
Regards!
You’re right! Updated the article, thanks 🙂