yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 7


3m read
·Nov 3, 2024

Hello guys, this is Macad101 with Java lesson seven. So, in this Java lesson, we're going to be going over a very important concept that eventually everyone needs to grasp called recursion. We're also going to be going over a little more on file streams, and so let's get started.

You know, open up Xcode and they're going to create a new project, Java Java tool, and I'm going to call it recursive app. Okay, now here's the thing: I'm not actually teaching you any new things at all today, but you're still going to learn something. Recursion is when a function calls itself, or something of that nature. So, say I have a public static void printRack, and then here say I have a string s and an int i. And just as an example, I will write out a little program that uses recursion.

Okay, and now say in my main function here, I call printRack again. Okay, now here's the cool thing, as you can see. I make this. I'll just make it read "start out." It will call this function right here. I will be 3, and S will be "again." This function will print out s, and then System.out.flush means that it refreshes the console. Then, it'll take one away from i, so now i will equal 2. Now it'll check if i is less than or equal to zero; then it'll not, it'll just ignore and close. Otherwise, it'll call itself and print again with i being 2.

So now we're back up here, s is still "again," and i is now 2, so we print it out, i minus minus. Now i is still greater than zero, so we're going to call it again with i being 1. It does this again, i minus minus. Now i is equal to zero, so this function closes. Then, it gets back to the last function that calls it, and that function closes, then the last function, and that function closes, then up here. And I'll just do System.out.println("done").

Yeah, now here's the cool thing: what this will make it do, of course, is print "again" three times, or maybe four. But either way, let's run it. And again, it did print out three times, and then it said "done." So this is an example of a recursive function; it is running itself. Okay, so this code in this function will freeze until it's done. The child function, and that child function is done. It's like an endless room of mirrors, except that it ends eventually.

So if you smash one mirror, all the other mirrors just stop, and that's how this works as well. That's how recursion works. So once something just closes and doesn't call itself, which happens when i is less than zero, then everything else closes. And this is the most important concept you can ever learn, in my opinion, because say you want to delete a folder.

There's a function to delete a file, and there's a function to delete an empty folder. So what you do is you'd have a function that deletes a folder, and in that function, it would go through every file and folder in itself. If it's a file, it would just call the function to delete the file. If it's a folder, it would call itself again on that folder, so that way we go into the next folder.

So this is actually a hard concept to grasp, recursion, but I think you may understand from this point. So the way recursion works is that a function calls itself. So this is a very small lesson. I just wanted to point out to you guys how recursion works, because if you can't think recursively, then you'll never do as many amazing things as you could be doing.

And so this is just an important lesson to learn on recursion. So anyway, thanks for watching, Mac kids, and unsubscribe for more Java lessons and such, and goodbye.

More Articles

View All
Using specific values to test for inverses | Precalculus | Khan Academy
In this video, we’re going to think about function inverses a little bit more, or whether functions are inverses of each other. Specifically, we’re going to think about can we tell that by essentially looking at a few inputs for the functions and a few ou…
'Big Short' Investor Reveals His Biggest Bet for 2024.
I’m always a little bit hesitant to try to ask you for Big Picture top down analysis, ‘cause a lot of times you don’t really want to go there. I actually have an opinion on this one. Good! If you don’t know that man, his name is Steve Eisan. He’s the seni…
Volume with cross sections: squares and rectangles (no graph) | AP Calculus AB | Khan Academy
The base of a solid is the region enclosed by the graphs of ( y = -x^2 + 6x - 1 ) and ( y = 4 ). Cross sections of the solid perpendicular to the x-axis are rectangles whose height is ( x ). Express the volume of the solid with a definite integral. So pau…
2015 AP Calculus 2c | AP Calculus AB solved exams | AP Calculus AB | Khan Academy
Let H be the vertical distance between the graphs of f and g in region s. Find the rate at which H changes with respect to x when x is equal to 1.8. So, we have region s right over here. You can’t see it that well since I drew over it. What you see in re…
How to Make it Through Calculus (Neil deGrasse Tyson)
Through it, I have a, I have a— I don’t quite call it elevated to the level of a parable, but it’s a story in my life that I reference all the time. Right now, I share it with you as short. I’m in high school, I’m a junior in high school and I want to ta…
"STOP DOING THIS If You Want To Be SUCCESSFUL IN LIFE!" | Kevin O'Leary
I’m the mean shark. I’m not the mean shark. I’m the only shark that tells the truth. My wife’s sitting there, and with my daughter, and he stops and says to them, “Hey, that guy from Shark Tank is sitting in the can, that [ __ ] Kevin O’Leary.” And Linda …