Core Java

OCA Java 8 Preparation : Lambda Expressions


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.

Functional Interface(FI) :

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 :

  • You can define any number of methods from java.lang.Object class in a FI.
  • It can also have any number of static and default methods.
  • You can optionally annotate it with @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 8 introduced many predefined functional interfaces in java.util.function package.
  • Some predefined FI in java.util.function package includes Predicate<T>,Consumer<T>, Function<T,R>, Supplier<T> etc.

Yes, you got it right ! Based on the definition of Functional Interfaces, existing interfaces like Runnable, Comparable etc are also Functional Interfaces.

Defining Lambda Expressions:

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 :

There are some interesting rules you need to know when writing a lambda expression.Let’s have a look!

Rules Related to Lambda Parameter List :

  1. The parameter list in a lambda expression is a comma-separated list of formal parameters usually enclosed in parentheses.
  2. If we specify the data types of the parameters in the parameter list, they are known as declared-type parameters. Or else when not specified, the parameters are referred as inferred-type parameters.
  3. In a lambda expression, you can have either all declared-type or all inferred-type parameters. Avoid the mix & match.
  4. When using inferred-type parameters, their type is derived from the type of Functional Interface used by the lambda expression.
  5. If you have a single inferred-type parameter in your lambda expression, you can skip the parentheses. Don’t ever try it when having a single declared-type parameter in your lambda expression. Will not work!
  6. When having multiple parameters in the parameter list, use of parentheses is mandatory, irrespective of its type – inferred or declared.
  7. Use of modifiers like final is only allowed for declared-type parameters.

Rules Related to Lambda Body :

  1. A lambda body can be either a single expression or a block of statements.
  2. Execution of a lambda body can optionally lead to return a value as an output or can even throw an exception.
  3. When a lambda expression is written in form of a ‘single expression’ , don’t use a return keyword. It can only be used within a block of statements.
  4. If you’re writing your lambda expression as a single expression, it can have any return type – void or non-void . Same is for the block of statements but in that case we write a return statement, if we plan to return some value.
  5. We must check the compatibility of the function type of the target type and the type of lambda expression.
  6. In reference to point 5, if lambda expression is expressed as a ‘single expression’ then the return value of that expression will be ignored if the function type of the target type returns void.


Conclusion :

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.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *