Java 8 Lambda Basics 6 - Introducing Lambda Expressions

Java 8 Lambda Basics 6 - Introducing Lambda Expressions

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 197

Number of words: 1464

Number of symbols: 6174

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
now does this solve this problem this is what I wanted to do right you wanted to pass in a behavior and then execute that behavior but it kind of does but I think there is some extra work that we are doing over here we are not just passing in a behavior we are passing a thing that has a behavior you know passing in a perform method there passing a greeting which has a method which is to perform wouldn't it be cool if we had just an action being passed in rather than a class that it determines an
00:30
action now here would it be cool if we just pass their action in and then this execute the action well lambdas set out to achieve just this lambdas lets you create these entities which are just functions they are called lambda expressions which are just functions which do not belong to a class they're not methods of a class there are just functions which exist in isolation and here's the best part those functions can
01:00
be treated as values this is kind of confusing for somebody who's totally used to object-oriented programming and they haven't done functional programming so let me explain what I mean by that what I mean by functions has values you know what inline values are right so you have let's say you have a string or double or an integer you can define them in life so here is an example of a string called name being declared and assign the value of foo foo is a string
01:31
which I have written in line and when this executes it takes this inline value and assigns it to the variable name so name contains a value which is the string fool this should be obvious similarly 3.14 is a double value which is assigned to pi so we know that data acts as values in Java you can assign it to variables we can assign it to different types similarly objects act as you a user in Java you can assign an instance of an
02:02
object to a variable but the question is can we assign a block of code to a variable as a value so the value is not the execution of the block of code does the block of code itself piece of code becomes a value that gets assigned to a variable and that wherever the variable goes a piece of code goes with it is that possible can we do something like this have a block of code which is the value which is the steps that need to be executed right here you can have lines of Java code is that possible now
02:34
let's take a look at this for a minute what is the standard way in which we write a block of code in Java before a Java eight of course well it's the method right you create a method which contains a block of code it has input arguments that has a return it says how you typically do it imagine if you could take a method and assign it to a variable and again note that I'm not saying execute the method and assign the return to a very pressing assign the method itself the method becomes a thing that gets assigned to a variable can you do that now let's say I have this
03:05
perform method this is the one we just saw right we have a perform at it which is a public returns a wide and it has system that print now what if I were to assign this to available call the block of code this is possible in Java using lambda you can write a lambda expression which does just this and once you do this you can take that variable a block of code and pass it around and have different other pieces of code executed which is really exciting so let's see how to write this lambda expression the code that you are looking at has a lot
03:37
of extra things which you don't need for instance let's take a look at this public public makes sense when a function is a part of a class in a method right you need to know if a method there's a public or a private or a protected because it makes sense in the context of a class but if a function exists in isolation doesn't make sense to call it public the function is accessible by whoever has that variable let's get rid of that next let's look at the name then you assign a string to a variable what's the name that you refer
04:07
that string by it's the name of the variable you don't have to give it another name similarly we can assign this function to available call the block of code the way you refer to this function is using that variable name you're going to use a block of code name to access the function which means that it doesn't need this other name get rid of that too now the creators of the Java spec could have rested here and say okay this is how you create a lambda expression but they actually went a step further and you see when you look at this code if I were to show you this code and say can you tell me what they
04:39
returned diapers you don't need to look at this wide you can actually look at the code and figure out what the return type is right it doesn't return anything so it's so Y so the Java compiler is now smart enough to do just that the Java compiler says hey if you're writing a lambda expression don't tell me what the return type is I can look at the code and figure it out if you have a return five over here the compiler knows yes this is something that return an integer if you have a return hello world here it knows is something that returns a string it's an object it knows what
05:10
type it is so the compiler will know what the return type is based on the expression itself so terms are in Java 8 you don't have to provide the return type over here for lambda expression so even that goes away so this is what's left these are the elements that you need to provide in order to write a lambda expression so we took a function that we know you know how to write that method right we started out with the per file method so we removed all the things that we don't need to specify when that
05:41
becomes a lambda expression you don't have to specify public for a lambda you don't have to specify the return type you don't have to specify the name so with that what's left is this parentheses which indicate the arguments and then the block of code okay so the syntax is pretty much this for a lambda expression except for one small addition the addition is we need to put this symbol over here you need to put that between the parenthesis and the block of code if you put this so there you go you've got your lambda expression
06:12
so if you know how to write a method in Java you not know how to write a lambda expression in Java you need to convert these steps in your mind till you get comfortable enough that you'll be writing these expressions directly in order to write a lambda expression take a method remove the modifiers right you don't need the public private you don't need the name of the method and you don't need the return type what's left is expression except for adding the symbol Brad which never looks like an arrow okay you put this between the parentheses and
06:44
the curly braces you have got your lambda expression of this lambda expression can be assigned to a variable you can actually do this in Java 8 and this variable contains the value which is that function right there is one further shortcut that you can do if your body of the lambda expression is just one line here just one language system dot out dot print so if that's just one line you can actually remove the curly braces so it just becomes this you have the parentheses the arrow and then that
07:17
line of coure but remember this is only if you have just one line of code in your lambda expression if you have multiple lines then you're gonna need the curly braces okay this is how you write lambda expressions

DOWNLOAD SUBTITLES: