Simple Lambda Expression
Start course
2h 28m

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this video we will talk about the Lambda Expression in Java. Lambda expression is a new and important feature of Java, which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in the collection library. It helps to iterate, filter, and extract data from the collection. If you remember, we talked about the anonymous classes in the previous video. One issue with anonymous classes, is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as one action should be taken when someone clicks a button. 

Lambda expressions enable you to do this; to treat functionality as a method argument or code as data. The previous section, anonymous classes, shows you how to implement a base class without giving it a name. Although, this is more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single method classes more compactly. Also, the lambda expression provides the implementation of the functional interface. An interface which has only one abstract method is called the functional interface. Java provides an annotation functional interface, which is used to declare an interface as a functional interface. Java lambda expression consists of three components. The first one is, the Argument-list. It can be empty or non-empty as well. 

The second is the Arrow-token. It's used to link arguments lists and body of expression. And the last one is the body. It contains expressions and statements for lambda expressions. Okay, after this information about the lambda, let's move on to the Eclipse and get some practice. First, I will create an interface. So, I will right click on the source folder in the object-oriented programming project, and select the new interface option. The package name can be lambdaexample, and the name of the interface will be Car and click the Finish button. Now I will create a method in this interface, void canGo(). Also, you can create this method with or without parameters. For example, I'll write two parameters. The first one will be the name type of string and the second will be int speed. 

Also, since this interface contains only one method, I'll annotate this interface as FunctionalInterface. Just above the interface, after the @ sign, I write FunctionalInterface. This is not mandatory but you can define it like that. Okay, now let's create the test class. So, I right click on the lambdaexample package and select the New Class options. The class name can be TestLambda, and I will check the check box for the main method, and click 'Finish'. Now I will call the Car interface in the main method. Car myCar equal. After the equal sign, I put the pair of parenthesis, and after the parenthesis, I put the lambda symbol. You can create it just by pressing the minus key on the keyboard and then pressing the greater than key. Okay, after the lambda symbol, this time I put the curly braces, and last, semicolon at the end of the block. 

As you can see we get an error, because the canGo() method we created in the interface has two parameters. So, I will write two parameters in the parenthesis to represent the parameters of the canGo() method. So, the first parameter can be name and the second parameter can be speed. You can write any name here. Okay, now the parameter represents the name parameter of the canGo() method, and the speed parameter represents the speed parameter of the canGo() method. Now, let's print a message to the console. System.out.println( name + " can go at" + speed);. 

Okay, now let's call the canGo() method using myCar object, myCar.canGo(), and we need to give the parameters of this method. The first parameter can be Ferrari, and the second parameter can be 300. Yes, now let's run and test the code. As you can see, the message is printed on the console successfully. Yes, the lambda expression in Java is like that. This is the simplest form of the lambda. We'll learn it in detail in the next videos. But for now, this is enough. The main purpose of the lambda is to provide the implementation of a functional interface, and do the same work with less coding. Okay, let's take a short break here. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics