Thumbnail for METHODS in Java are easy 📞 by Bro Code

METHODS in Java are easy 📞

Bro Code

15m 22s2,207 words~12 min read
YouTube auto captions
Transcript source

YouTube auto captions

This transcript was extracted from YouTube's auto-generated caption track. The transcript below is server-rendered so it can be read, searched, cited, and shared without opening the original YouTube player.

Timestamped outline
Pull quotes
[0:00]Think of a method as a block of reusable code that is executed when you call it.
[0:00]Well, with a method, you can write some code once and reuse it whenever you want.
[0:53]If I need to sing Happy Birthday, let's say, three times, well, without using a method, we could, although it's inefficient, copy this code and paste it two additional times.
[0:53]So, to create a method in Java, outside of the main method, do pay attention to these curly braces, we'll create a new method.
Use this transcript
Related transcript hubs

[0:00]What is going on everybody? In this video I'm going to discuss methods in Java. Think of a method as a block of reusable code that is executed when you call it. To call a method, you use a set of parentheses. Kind of like it's a pair of telephones talking to each other. Why are methods useful? Well, with a method, you can write some code once and reuse it whenever you want. You just have to call it. Let me show you why a method is useful. So let's say we have a task. We have to sing a Happy Birthday song three times. So, without using a method, we can do something like this. We'll create one verse of a Happy Birthday song. I'll just make something up. Let's say, Happy Birthday to you!

[0:46]Happy Birthday, dear you!

[0:53]You are x years old! Then, Happy Birthday to you again. Then I'll add a new line character to the end. So, when we run this code, we sing Happy Birthday once. If I need to sing Happy Birthday, let's say, three times, well, without using a method, we could, although it's inefficient, copy this code and paste it two additional times. Then we're singing Happy Birthday a total of three times. Well, you know, this does work, but we tend to be repeating ourselves a lot. In programming, we like to follow the D.R.Y. principle, Don't Repeat Yourself. How can we not repeat ourselves if we don't have to? Well, let's create a method. A method is just a block of reusable code. So, to create a method in Java, outside of the main method, do pay attention to these curly braces, we'll create a new method. A method can return something. For now, we'll type void, much like our main method. And we need a name for this method. The method should be descriptive of what it does. Let's call it the happy birthday method. All it does is sing Happy Birthday. We need a return type, a name for the method that's descriptive, parentheses, then curly braces. Let's cut our song of Happy Birthday and paste it within the Happy Birthday method. At any point, to use this method, I have to call it. Call it by its name: happyBirthday, then add a set of parentheses. So, we have one issue in Java. Java wants this method to be static, much like the main method that we work within. So, in order for this program to work, we need to add this keyword of static. Normally, you don't need this keyword, but since we're calling this method of happyBirthday from another method that's static, this method also needs to be static. Later on in the series, we'll discuss what static does. You'll likely have to include that for now. All right, let's do a test run. So, we are going to sing Happy Birthday once. So again, to call a method, type the name of the method, then a set of parentheses. The pair of parentheses kind of represent a pair of telephones. That's how I think of it at least. So, now we should sing Happy Birthday. Now, this code is reusable. We write it once and reuse it whenever we want. We just have to call it again. We will call the happyBirthday method again. Now, we are executing that block of code two times, or even three. Happy Birthday.

[3:38]One thing you should be aware of is that methods are unfamiliar with any variables declared within other methods. So let's say we have two variables, a string of name, type in your first name, whatever it is, and an integer of age. Set it to be some age. If I was to use these variables within my happyBirthday method, well, the happyBirthday method actually doesn't know what these are. So, let's attempt to use them and see what happens. Within our happyBirthday song, let's convert this print statement to be a print F statement. And we will insert our name variable. Replace "you" with the format specifier, and we are displaying a string. See, we already have a warning: cannot resolve symbol 'name'. I'll attempt to run this code and we'll see what happens exactly. Java cannot find variable 'name'. Methods are not familiar with variables inside of other methods. If we live in this method of main, if it's our house, so to say, we can't see what's going on in our neighbor's house. One way in which we can solve this is to pass arguments to this method. From one method, we can send information to another method. These are known as arguments. So, when we sing Happy Birthday, within the set of parentheses, within the pair of telephones, we can pass a value or a variable. So, let's send our variable of name to this method of happyBirthday.

[5:25]Anything that you send a method via arguments, you need a matching set of what is called parameters. Our happyBirthday method isn't set up to receive these arguments. We need a matching set of parameters. To set up some parameters, we're going to list the data type of what we're receiving. We're receiving a string value. So, the data type is going to be string. And then we need a name for what we're receiving. It doesn't have to be exactly the same, but it might be helpful to keep these consistent. So, let's perform a test run. After this print F statement, I'm going to add a new line character. Happy Birthday to you! Happy Birthday, dear Bro! You are x years old! Happy Birthday to you! So, if I was to change my name to let's say, SpongeBob, we'll insert this value at this location within our method. Happy Birthday to you! Happy Birthday, dear SpongeBob! You are x years old! Happy Birthday to you! Let's also pass in our variable of age. So, any arguments you're sending to a method, you're going to comma separate. However, we need a matching set of parameters. Our method isn't set up to receive this variable of age. Let's perform another test run. So, we have an error: actual and formal argument lists differ in length. We need a matching set of parameters to the arguments we're receiving. Not only do we need a string variable, we need an integer variable because we're receiving an integer. So, let's say int age. We'll insert our age variable right here. We're displaying an integer, so we need percent d. This will be a print F statement. Then I will include a new line character. We are inserting our age variable. Let's say SpongeBob is 30 years old. All right, let's see how this works now. Happy Birthday to you! Happy Birthday, dear SpongeBob! You are 30 years old! Happy Birthday to you! So, with these parameters, you can rename them. When you receive these arguments, just be sure that the order and their data type is correct. For example, we could rename name as, let's say, Birthday Boi.

[7:51]You know, this would work too. SpongeBob is the birthday boi. The names of your parameters can be different from the arguments. Just be sure that you get the data type correct and the order in which you receive these arguments. Let's replace the name of SpongeBob with Patrick. Patrick will be 38. So, now with the happyBirthday method, we're inserting new variables. Happy Birthday, dear Patrick! You are 38 years old! With methods, they also have the capability to return a value. So, I'm going to collapse this happyBirthday method. We won't be working with it anymore.

[8:36]Let's create a method to square a number. We'll create a method to square a number, then return the result. Normally, if we're not returning anything, we would type this keyword of void. But since we're returning a double, we have to list the data type of what we're returning. We're returning a double. Then we need a name for this method. Let's say double square.

[9:01]All we're going to do is use the return keyword. Let me zoom in a little. Let's return a number times itself, number. We're going to receive one argument. An argument of numbers. We have to set up a matching parameter. Our number is going to be a double. Double number. Let's call the square method. I will square the number three. And again, since we're calling a method that's not static from another static method, we need to add this keyword of static. In order for it to run. There'll be more on the static modifier in the future. So, if I was to run this, we get no output. We're returning a double back to the exact spot in which we call it. We could assign it to a variable or print it directly. Let's say, double result equals square the number three. Then display the result. So, three squared would give us nine. So, when returning something, just imagine that after the method ends, we're replacing that method call with whatever we're returning, in this case, nine. Picture it that way, and then we're assigning nine to our double of result. Or, we could just output it directly, if you would prefer to do it that way. Let's print three squared.

[10:27]And that would also give us nine.

[10:31]Now, let's create a method to cube. We're going to return a double. Double cube. We have one parameter, a double of number. We're going to return our number times number times number. That would cube a number that we're given. And again, we need to add static because we're calling a method from another method that's static. All right, this time we will call the cube method and return the result. So, three cubed is 27. 3 * 3 * 3 is 27. One last example. We'll create a method to return a full name after being given a first name and a last name. We're going to return a string. We'll call this method getFullName. We'll have two parameters: a string of first, for first name, and a string of last for last name. We're going to return our first name plus a space to separate them, plus our last name of last. And again, we have to add static. Okay, let's declare a string variable of fullName equals call the getFullName method. Then pass in some arguments. Let's pass in SpongeBob, then for the next argument, SquarePants. And then we'll print the full name. Print fullName. Our method returns one long string of SpongeBob SquarePants. The return keyword will return a value after the method ends. It will return that value back to the place in which you called that method. After this method of getFullName is complete, just imagine that we're replacing it with one long string of SpongeBob SquarePants like this. And then assigning it to this variable. Okay, so I lied, I have one last good example to give you. We'll create a method to verify a user's age. Normally in the United States, at least, you have to be 18 or older to sign up for things, like a credit card. We'll create a method to check a user's age. Again, we're going to use static because we're calling a method from another method that's static. Let me zoom in. We're going to return a boolean this time. The name of the method, let's name it ageCheck. And there's going to be one parameter, an integer of age. If we're going to do an age verification check, we could use an if statement. So, within our method, we'll write an if statement. If our age parameter is greater than or equal to 18, then let's return true. Else, we're going to return false. This can be simplified to the following. Return age is greater than or equal to 18. You can do it either way. For a beginner, it might be easier to read if we have separate if/else statements, but both work the same. All right, so we now have a method to check a user's age. So, now we can use this method. I will create an integer variable of age. Set it equal to some age. I'll use an if statement. We're returning a boolean value. I can use that within an if statement. If ageCheck, then pass in an argument. We will pass in our variable of age. If this is true, if age is greater than or equal to 18, we return true. So, let's print the following. You may sign up! Maybe we're signing up for a credit card. Else, let's print, you must be 18+ to sign up. Our age is 21. You may sign up! But if we were 12, that method returns false. You must be 18+ to sign up. So, that's a method everybody. It's a block of reusable code that is executed when called. Again, I like to think of the parentheses after a method as a pair of telephones talking to each other. To use a method, you have to call it, and well, everybody, those are methods in Java.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript