DiscoverCoders Campus PodcastEP51 – Let’s Talk Lambdas in Java
EP51 – Let’s Talk Lambdas in Java

EP51 – Let’s Talk Lambdas in Java

Update: 2021-11-12
Share

Description


In this episode we'll talk about a super useful feature that was introduced back in Java version 8, known as Lambdas.


The Lambda feature is something you didn't know you desperately wanted or needed until you understood it.


The Lambda syntax allows you to write much cleaner and more readable code, while also empowering you to get more done with less code.


In this lecture, I'll be referring to some code that you can download via this github repository.


Episode Transcript


0:00

Hey fellow coder before we rolled into the episode today this one is sponsored by brought to you by the Coders Campus Bootcamp. If you haven't already checked it out, you can check it out via coders campus.com forward slash boot camp. So if you're looking to get a job as a coder, and you've been struggling to achieve that on your own, you are struggling to learn how to code on your own. You're not alone. And that's what that's why these boot camps are so popular. So we'll talk more about that at the end of this episode. But yeah, please do check it out coderscampus.com/bootcamp, you can apply to see if you'd be a good fit. So having said that, let's roll into today's content


0:48

Welcome to the coders campus podcast, where you'll learn how to code from one of the best teachers in the industry. Whether you're an absolute beginner or a seasoned pro, the coders campus podcast will teach you what you need to know to master the art of programming.


1:03

And now, your host, Trevor page.


1:07

Ladies and gentlemen, boys and girls, children of all ages. Welcome back to this 51st episode, I think of the coders campus podcast. Thank you so much for joining always a pleasure to be here in the hot seat and bringing you the important information curated by none other than the No. Okay, so let's focus. So yeah, welcome to the podcast. Great to be back. And I want to kick things off by talking about sort of what I alluded to in a past episode, I forget when I alluded to it. But yeah, I want to get to talking about streams eventually. But before I can talk about streams, I want to talk about lambdas. So today's episode is always good, it's going to be I should say always going to be the full episode will be about lambdas and lambda expressions and lambda, this lambda that you will be by the end of this episode, a lambda expert. So let's dive right in to it. I'll open up my notes here. So I guess the first thing we can start off with is what the heck is a lambda? Why is Trevor talking about this subject? When is it used? Why is it important? Why should I know about it, that kind of thing. So we'll start off by obviously talking about what a lambda is. So it's essentially a syntax that is used to cut down on the code that you have to write essentially, reduce the verbosity is the professional term, if you will reduce the verbosity of the code. So what that means is, you'll have to write less code in order to do the same thing. Okay, that's really, you know, there's a lot more than it can do. But that's, I mean, you know, fundamentally, why would you choose to use a lambda over not a lambda, it's, it's really to reduce the verbosity of the code. And that should tend to lead to more readability case, your code becomes shorter, more succinct, and more readable. Now, the it's a double edged sword. Because when you do this, when you reduce the verbosity, when you abstract away some details, when you hide the inner workings of what's happening through shorter code, although it's more readable, it's only readable if you understand it. So what this this tends to lead to is more confusion if you do not understand it, hence, me talking about lambdas. Today in this podcast episode, so lambdas are very powerful. They were a feature that was added to the Java language as a version eight. So Java, version eight at the time of this recording, is, you know, one of the most, if not the most popular version of Java being used out in the wild. And no, some you know, there's no surprise there's there's a big correlation between lambdas being introduced in version eight and version eights, popularity, lambdas, and streams were very well received, and in my opinion, are great, I absolutely love them. So important to learn about. Now beyond reducing the verbosity of the code with respect to using lambdas. Beyond that, we also it allows us to treat


4:25

functions, kind of like parameters, kind of like you do in in the world of JavaScript with JavaScript, you can pass functions around as parameters if you don't know JavaScript. And I guess that's not very helpful to you. But if you do know JavaScript, and you've done this before, then great. You can sort of think of lambdas sort of unlocking this functionality, if you will, of being able to pass a function around inside of the method signature.


4:52

So yeah, it allows us to treat methods like parameters, sort of, so


4:57

basically, get the way to think about this


5:00

is in my mind is to look at how we had to do it the old way. And and what we had to do in the pains and whatnot of what we had to do the old way before version eight. And then we can look at what the new way is with respect to lambdas. And, and whatnot. So I do have some code. And I will try to, I guess I'm gonna have to put the code in some show notes for this episode, so that you guys can load it up and look at it. Again, I'm tasked with trying to explain code, verbally, without you guys seeing the code in front of your eyes, which is always as I say, a very difficult task. But if you are not driving, and you're able to, you can go to I guess I'll put it up coderscampus.com/51. As this is Episode 51, you can go there and bring up the show notes. And you'll be able to see an example of what I'm doing. I'm also going to post this or I have already posted this as a GitHub repository. So you can also not just see it on the blog, but download it and play with it. Because that's what all the cool kids are doing these days. So


6:07

yes, having said that, let's dive into sort of the code. So the old way of I won't say the old way of using lambdas. But in that's not correct. There were no lamb lambdas in the olden days before Java eight. So how did you do this? How did you work? You know, with these things? Well, basically, what ended up ends up being is you are working with interfaces, and you're trying to


6:33

utilize or use an interface by sort of instantiating it. So when you try to instantiate an interface, if you have done your homework, you know that that's really kind of not possible, you cannot instantiate an interface, you can only instantiate a concrete class, right? So you can only instantiate a class. So how can you get around this? How do you get around


6:56

the whole you can't instantiate an interface, but yet you kind of can instantiate an interface.


7:03

It's done through essentially


7:07

instantiating the interface and overriding it. So in other words, you are implementing it right there. And then inside of the code. So an example of this, you probably swatch. And so you probably seen, if you worked prior to Java eight, you will have seen this through the syntax of when you say a new interface. So if you tried to list like a list, or a map, or whatever, and you say new list, and try to instantiate a new list, list interface, or map is an interface, those are not concrete classes, so you can't instantiate them. But what you will see is, if you say new list or new map,


7:44

and then you try to, you know complete it, what it's going to do is it's going to open up a body of code, it's going to open up curly brackets. And it's going to force you to override each and every one of the well abstract methods inside of the interface, right? Because that's all an interface is made up of, if you want a quick review of what an interface is an interface is made up of public abstract classes, or sorry, public abstract methods, not classes methods. So an interface is made up of public abstract methods. And what does that mean? What is a public abstract method? It's just a method that has no body. Okay? It has nobody has nobody to turn to terrible joke, I'm sorry. There's no body of the code. So basically, what's happening here is when you try to instantiate an interface, it says, Fine, I don't know what you're doing. I don't know why you're trying to instantiate this interface, you can't do that. So here's the workaround, I'm going to force you to override and create the body of every single abstract method for me, because that's the only way you can use an interface, right? You have to give it behavior, you have to override the code and give it a body. And this is usually done. In a separate class, you have a concrete class that implements the interface, right? You have a class implements the interface, and that class is overriding every one of the methods for that given interface. And you'd have it all in one neat place in one class file. Well, when you do it on the fly, if you're doing it in line, if you will, if you're saying, you know, some if you say list, my list equals new list, it's going to say, Oh, well, we're not going to put this in a different class file. We're just going to make you do it right here in inline in the code right now, next to the new keyword right next to the new list, parentheses, whatever, then it's going to force you

Comments 
00:00
00:00
x

0.5x

0.8x

1.0x

1.25x

1.5x

2.0x

3.0x

Sleep Timer

Off

End of Episode

5 Minutes

10 Minutes

15 Minutes

30 Minutes

45 Minutes

60 Minutes

120 Minutes

EP51 – Let’s Talk Lambdas in Java

EP51 – Let’s Talk Lambdas in Java

Trevor Page