Lambda expressions in Java are a simplified syntax that allows you to define new functions outside the context of class methods. In this video, learn what Lambda expressions look like, as well as how to use them to create new functions.
- [Narrator] So we just saw how the concept of first class functions in Javas function interface allow us to get a reference to the method of another class and work with that method just like any other object. And as I mentioned for those of you coming from a strictly object oriented background this might seem like quite an interesting concept. However it gets even more interesting because in Java not only are we allowed to use the function interface to create references to existing functions on other classes but we're also allowed to use the function interface to define new functions from scratch. And to do this Java provides us with a new piece of syntax called a lambda expression. And here's what they look like. Basically a lambda expression is a shorthand syntax that we can use to define new functions without having to define these functions as methods of any given class. Lambda expressions start off with a set of parentheses, which contain whatever arguments the function will accept and then they have an arrow followed by whatever we want the return value of the function to be. And note that when our lambda expressions are only one line, we don't need to use the return keyword here, the value of the statement after the arrow is returned automatically. So here we see an example of what it might look like to actually use a lambda expression to define the new function using the function interface. Now when we define a new function using a lambda expression, the types that we provide in between the triangle brackets of the function interface have to match the types of the argument and return values respectively. So in the case of defining a function that takes integers and argument and returns that integer doubled plus one as we see here the two types we supply here would be integer and integer. And note that since we've already supplied the type of the argument inside the triangle brackets, we don't even need to supply that again inside the parentheses of the lambda expression. Here we see another example where we have a function that takes a string as an argument and returns its length. And here are the two types that we supply inside the triangle brackets would be a string for the type of the argument and an integer for the return type of the function. And another thing that you should know about the lambda syntax is that in the case where we have only one argument we're allowed to actually drop the parentheses around that argument. So it'll look like this. Now one last thing to note is that it is possible to have lambda expressions with multiple lines. In that case you just have to wrap the body of the expression in curly braces and use the return keyword on the last line, assuming that you want to return whatever the value of the last line is. So that's a very basic introduction to lambda expressions in Java. A little later on in the course we'll see how these expressions can be extremely useful in functional programming as well as in some cases reduce the amount of code that we have to write. But before we move on, just to get more comfortable with lambda expressions, let's code out a simple example. And again we're going to be writing this example inside the main method of our class here so that we can immediately see the results. So for this example let's use a lambda expression to write a function that returns the absolute value of an integer. So what we're going to do is we're going to start off by importing the function interface again. So import java.util.function.function. And then we're going to say function and this function is going to take an integer as an argument and return an integer. And we're going to call this function absolute value. And then to define this function we're going to use a lambda expression, which will take an argument which we'll call X and then it'll simply return we're going to use a ternary operator here. It'll say if X is less than zero then return negative X. Otherwise return X. So that's how to do it all on one line, if you wanted to you could make it a little more readable by doing a multi line lambda expression. So we could say X in curly braces and say if X is less than zero return negative X. Else return X. And now that we've defined this absolute value function here using the lambda function syntax we can call it and we're going to print out the result here by the way. We can call it by saying absolutevalue.apply and then we could apply it to whatever number we want. And let's run our code now. And we see that we get the correct result printed out.