Methods

Learn the Basic Java Concepts Loops, arrays and methods
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
Already have an account? Log In

Transcript

Hello there, and welcome back to this Java development course. So, last time we went over sorting arrays and yet tomorrow to do your job was to modify, modify our sorting arrays algorithm that we built together in the last video to sort the numbers from highest to lowest instead of from lowest to highest. So here's essentially what should have done. You may not even see what I changed. Because it's literally one thing right here. No, no right here.

There we go. This is the only thing you need to change. So if we do it like this, this sorted from lowest to highest if we do it like this, this sorted from highest to lowest. Why? Well let's take a look. So here, when it when it's like this, then it checks is for more than three.

So is a one actually goes through Let's keep like this for now is for more than three, so is AI one more than a one plus one. So is the letter on their left more than the letter than the number on the right. In this case it is. So it switches three and four. If on the other hand we have it like this, it checks is AI one less than four. So in result, it will switch if three is less than four into one result we get four three, then it checks if three is less than two it's not then it checks if two is less than five and so on.

But the only thing we need to change so just this letter here, that's the only thing we need to change. This is more than this is less than pretty simple. If you understood bubblesort then you understood how to do this. Anyway, without further ado, let's get to the lesson. Alright, so today we're going to be going over methods and methods are actually pretty Cool, I guess they are, they are pretty cool. It's pretty new class it can be called methods.

And main as always, and main method is always okay. So what is the method? Well, a method is just a collection of code. So let's just say that we have a code, right? We need to print printed array. Okay?

So we have an array here into array into array A equals one, you know, some numbers, okay. Now we need to print that array. Well, we can't just do system I'm pulling array we would have to do for int i equals zero. i is less than a dot length i plus plus, and then system dot, System dot out dot print ln AI, right? Now imagine we have an application that has a couple hundred arrays and we to print those arrays a couple thousand times. So in our application, we would have to put the print array this printer a code a couple thousand times, right?

Since it's just that we're going to be printing an array that many times what are we going to do? Are we just going to copy and paste this code in? No, no, that's that would take up a lot of space. So every time we copy and paste it every single time is wasted space. So in result we have something called the method. So we can do public void, print array, integer array, and paste that in and there we go.

So now if you want to print the array, all we do is print array Hey, Okay, we also can make it static as well, there we go static, there we go. printer a. And so now, if we run this There we go. As you can see, it prints the array. So what happens? Well, when we do print array, it calls this method.

And it inserts this array into here. Then this code is executed as if everything was normal. And remember, this interest here is what we entered here. So let's take a look at a little bit of a easier example just so we can get a better understanding. So let's just say public, static, void, let's say hello world. Okay.

All we're going to do here is just do System dot out dot print ln. Hello, world. There we go. And now if we call this hello world method in Maine, What will happen is it will run this code as you can see it prints hello world. Now, I'm sure you're wondering why Maine is a special method and Why hello world is not so why is it that Java chooses to automatically run main whenever our application starts and not hello world? Well, that's because reasons generally speaking in most programming languages, by convention, they start with the main method.

So whenever you run a run a file usually looks for the main method and then runs that it's the same in c++ as well, I think and, you know, many other languages. So yeah, that's the generally how, how, how things work. So yeah, it just chooses main just because that's what the Java creators decided. Okay. So that essentially how methods work now, there are two things to understand with methods. actually three things.

The first is just generally how they work. So three parts to the method. So this is the first is the body. So the body is actually just this system out println hello world. Now, the second is what we insert here and what we already inserted, so we can have, for example, string to print, let's actually create a print method together. So we're gonna have a string to Print to print.

There we go. And here, we're just going to Print to print. Okay, so now, what if we wanted to use our method? That is, instead of writing System dot out dot print, ln, what do we just want to print and then just something without writing the system dot out? Well, we would just use our method we would do print hello, world. There we go.

All right. And now if you run this, there we go. It prints hello world. So the point is, when we insert something into the public parentheses it is inserted into here. So, if we have a string to print, we can now do, we're actually now we are ready to print it. So yeah, we insert hello world into here.

So this is so doing this essentially the same exact thing as just doing to print equals and then hello world, this is the same exact thing. Okay. And then the last thing that we need to understand with methods is the return. So right here we have the void. Void essentially means that it returns nothing. What is returning even mean?

Well, why should he returns an int? public static into print? Okay, and now we just do return one. All right, and there we go. So now we can do int a equals this and then Now we can System dot out dot print ln A. There we go.

So now as you can see, it just returns one. And when we actually do, it's just going to print eight here. All right, yeah, since its interior, like this, there we go. Okay, and now, that's essentially how it works. So when we do int a equals print, hello world, whatever it returns is assigned back into int a, in this case is going to be one. So it's going to be used, for example, let's say that we're printing something to the console.

And if it was successful, so we printed it successfully, then we return one if we printed it unsuccessfully, then we return zero, for example. It's like it's an error code, sort of. There's an example of where we can use it. Of course, there's a lot of other place we can use it and then avoid is just if we don't return anything. Alright, so those are the three very basic parts of a method. Now let's take a look at some of the some details.

Alright, so first of all, we can actually have multiple methods with the same name. So we can have public static int, print in two print. Whoops. All right, and then we can do System dot out dot print ln to print and then return one. There we go. All right.

So as you can see, this method has the same name as this method. Now, this point, you might be wondering, then how does Java know when we call this print method? How does it know that we are actually calling a method you know, one of these methods Doesn't know if we're calling this one or this one? Well, because there's a different inserted code here. So in this case is going to be a string, and this case is going to be an int. So since we put a string in here is going to automatically run this one.

So we actually return true to here. And you'll see that it actually ran this print method, and not this one, it will return one. But if we insert an integer into here, let's say like 12. Now, it will return one since it ran this method here and not and not know, it ran this method here, not this one. There we go. All right.

So now what we could also do is we can, for example, change this to string. And now it'll give us an error. So we can have methods with different names, as long as they have different inputs. Okay. All right. Right, we can also have multiple input so we can do string to print and then also string.

Number one, let's do int number, int number. And now we can go ahead and return number. Okay, so we can now do. Let's just say, for example, notes, let's put that back. We're going to do this and the number will be there we go. Alright, so now when we run this, as you can see, it will return a, in this case, and depending on whatever we'll actually know, in this case, completely completely completely wrong here, I have to put a string hello.

And then as the number let's put 678 and now as you can see it returned 678. All right, so there we go. So those are two details. So the first one That you can have multiple methods with the same name as long as they have different inputs or different input types. And then we can have multiple inputs as well. Alright, so, a couple more details First of all, whenever we return something nothing else will get executed under that return.

So, if we then put System dot out dot print ln HelloWorld I think eclipse will actually give us an error Yeah. Hello World. innocuous unreachable code or Yeah, unreachable code. So, after we return something, it just stops executing so nothing will get executed after the return. Okay. Pretty simple.

And then also we have the return nothing. So in case we have a void, we actually can just do return. And in this case, we don't need to return anything. So If we had an integer here, we would have to return something. So an integer. But since we don't have it is void, we can just do return and that's it.

Okay. And also as me, as you perhaps you may have seen, we have to have a return. Yeah. So we have to have a return otherwise we get an error message Marshall Turner's it'll type of type int, if, of course we have a return type. So if you have void, then we don't need to return anything. All right.

Okay. All right. So methods, methods, that's I think that's pretty much it. So we're gonna have a different another lesson where we're going to go over methods and methods, part two. We're going to go over recursion and some other things that we can do with mesh methods, just some, some more interesting things. But this is the very basics of methods.

So yeah. Anyway, without further ado, let's assign some homework. Alright, so homework is going to be very simple. Just create four methods which will called any method, it's going to print the other four. So, all right, so just to give an idea, I have four methods, right. So method one, method two, method three and method for each of them are going to print their corresponding method number.

So if I have, for example, method one is going to print one, method two is going to print two, and then method three is going to print three, and so on. So if I call method one, it's going to print 123, and four, so it's going to print its own method number and then call all the other methods. And then same with all the other methods as well. Now, the point is, if the method is called from main, the program should print the method and then call all the other methods. If the method is called from within another method, it should just print the corresponding method number and not call any other methods. So it's a little bit tricky, but if you really Think about it, you can get it and yeah.

So anyway, without further ado, I wish you luck and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.