Transcript for:
Lecture on Lambda Expressions

now let's try to use Lambda expression here so what we are doing is we have a functional interface and then we have discussed about it functional interfaces interface which only has one method and then we were able to instantiate a with the help of a Anonymous inner class and yeah we were able to call the method now how do we use Lambda expression here now Lambda expression says we just simplify the anonymous inner class okay only for Lambda expression now see if I tell you okay just try to imagine this if I tell you that there's an interface called a and there's a method called show so in your mind you know there is a interface a which has a method show and if I ask you to do this if I say try to complete the sentence if I say a obj equal to now in your mind you know a obj equal to means there should be new a round bracket 30 packets open and then public void show now how you know that this is new a because it's a object right we're trying to instantiate the interface here of course you can't instantiate the interface but we're trying to instantiate a class of interface here a class which implements the interface so basically when you say aobj this is something you can imagine right there is something you can think about in the same way now since you're talking about the interface a which only has one method you also know that the method name is show which returns nothing it's a white type and it's also public by default so from here till here you know it right so if you know it even compiler should know this right why you as a programmer need to type this code what if Java says hey don't worry I'm there you just don't have to type all those things you just say back it open I will understand what you're trying to do the only thing is you have to put that uh Arrow here that's it this is called a Lambda expression this Arrow here is a Lambda okay and this is the entire expression we have okay so just to redo the same stuff what we are doing is we are removing from here till here and since we are removing this one curly bracket here we have to remove this one as well so first let's remove that Curly bracket and then we'll remove the entire part from here and then we have to put a arrow so by doing this we are asking our compiler hey I will not type the code I will simply say aobj equal to it's your job to complete the sentence see behind the scenes a compiler will do it for you it's there behind the scene new a public void show is there it's just that syntactically the code is getting reduced this is called a syntactical sugar right where you are reducing the code okay so this is how it looks like and also if in this method you only have one statement so if you have multiple statements you need curdly packets but if you have only one statement you can skip that as well if you can skip this semicolon as well because it's not doing anything and you can write everything in one line and look at the beauty you can reduce the code just like that and I know you still have that doubt in your mind will it this work let's try and it's working you see that this is Lambda expression okay can we do something more with this of course I want you to re-watch this video The Cutting part and try it out now let me can we do something else here what if the show method actually accepts a value which is let's say int I okay and then from here also you have to pass the value the only problem is how will you accept the value here now instead of making the changes here what I will do is I will just go back to our previous syntax so that I can show you step by step what we are doing okay so again I'm passing a integer I here and then we have to pass the value here now this is not a method which is not taking any parameter this we are taking a parameter so I have to accept it here as well so you have to say int I on any variable it doesn't matter and then whatever you want to do with that variable that's your choice let's say I want to use it it's your choice if you want to use it or not but I'm printing in show I the value whatever the value of I is which is 5 in this case let's try to run this code to see is it working yeah it is working you can see we got five now can I use a Lambda expression here and the answer is yes what you can do is again from here till here we don't need that and we can remove this curly packets as well the only thing I can do is you have to put a arrow here we don't need this semicolon we can just put everything in one line and that's what we have done and yes if you even if you have variables you can do that not just one variable if you have multiple variables uh you can put multiple variables here let's say a into J as well and then here also you can accept into J just it's just that you have to pass two values let's say five and eight we're not doing anything with J but it's your choice if you want to use it or not as of now we are working with only one variable so let's stick to that let's pass only one value and let's write only one let's see if this works compile and run it works you can see that we got in show five so there's one more thing you don't even need to mention the type of the variable you know why is because we have mentioned it here right when you know that the variable type is end why to mention it okay and one more thing if you have only one variable you don't even need this round brackets you can simply mention the variable name here oh we have reduced the code so much okay so let's try to compile this code and run it works right now let's do something more with this now let's say what if you have a method which returns something what if you have a method which accept multiple values so let's try to do the example for that in the next video but if you see the files here okay this is the older one let me just delete all the class files just a keep it simple delete and let's recompile the code here compile the moment you compile you can see it is creating only two files a DOT class and demo.class of course behind the scene there is anonymous class but it is not creating a class file for you so it's so it saves the amount of files which you create of course the demo.java file will be quite heavy now if you compare the file size before using Lambda expression after using Lambda expression the demo file size will increase but it will not create a new file okay so let's files to manage okay so that's how we use Lambda expression here but what if you have a written type that will say in the next video