Understanding Return Type

Complete Step By Step Java for Testers Classes And Methods -> Object Oriented Concepts
10 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.80
List Price:  €131.33
You save:  €37.52
£80.29
List Price:  £112.41
You save:  £32.12
CA$137.56
List Price:  CA$192.59
You save:  CA$55.03
A$155.72
List Price:  A$218.02
You save:  A$62.29
S$136.13
List Price:  S$190.59
You save:  S$54.46
HK$783.15
List Price:  HK$1,096.44
You save:  HK$313.29
CHF 90.82
List Price:  CHF 127.16
You save:  CHF 36.33
NOK kr1,103.56
List Price:  NOK kr1,545.03
You save:  NOK kr441.46
DKK kr700
List Price:  DKK kr980.03
You save:  DKK kr280.02
NZ$169.65
List Price:  NZ$237.52
You save:  NZ$67.86
د.إ367.23
List Price:  د.إ514.13
You save:  د.إ146.90
৳10,973.26
List Price:  ৳15,363.01
You save:  ৳4,389.74
₹8,346.81
List Price:  ₹11,685.87
You save:  ₹3,339.05
RM478.30
List Price:  RM669.64
You save:  RM191.34
₦129,687.03
List Price:  ₦181,567.03
You save:  ₦51,880
₨27,846.09
List Price:  ₨38,985.64
You save:  ₨11,139.55
฿3,684.97
List Price:  ฿5,159.11
You save:  ฿1,474.13
₺3,259.29
List Price:  ₺4,563.14
You save:  ₺1,303.85
B$524.14
List Price:  B$733.82
You save:  B$209.68
R1,920
List Price:  R2,688.08
You save:  R768.07
Лв183.64
List Price:  Лв257.10
You save:  Лв73.46
₩138,024.19
List Price:  ₩193,239.39
You save:  ₩55,215.20
₪378.55
List Price:  ₪529.99
You save:  ₪151.43
₱5,756.02
List Price:  ₱8,058.66
You save:  ₱2,302.64
¥15,447.20
List Price:  ¥21,626.70
You save:  ¥6,179.50
MX$1,726.94
List Price:  MX$2,417.79
You save:  MX$690.84
QR366.07
List Price:  QR512.51
You save:  QR146.44
P1,385.95
List Price:  P1,940.39
You save:  P554.43
KSh13,348.66
List Price:  KSh18,688.66
You save:  KSh5,340
E£4,832.56
List Price:  E£6,765.78
You save:  E£1,933.22
ብር5,710.80
List Price:  ብር7,995.35
You save:  ብር2,284.55
Kz83,585.80
List Price:  Kz117,023.47
You save:  Kz33,437.66
CLP$96,622.33
List Price:  CLP$135,275.13
You save:  CLP$38,652.80
CN¥724.04
List Price:  CN¥1,013.69
You save:  CN¥289.64
RD$5,929.75
List Price:  RD$8,301.89
You save:  RD$2,372.13
DA13,457.15
List Price:  DA18,840.55
You save:  DA5,383.40
FJ$227.57
List Price:  FJ$318.61
You save:  FJ$91.03
Q780.47
List Price:  Q1,092.69
You save:  Q312.22
GY$20,930.40
List Price:  GY$29,303.40
You save:  GY$8,372.99
ISK kr14,100.58
List Price:  ISK kr19,741.38
You save:  ISK kr5,640.80
DH1,016.63
List Price:  DH1,423.32
You save:  DH406.69
L1,790.82
List Price:  L2,507.22
You save:  L716.40
ден5,785.11
List Price:  ден8,099.38
You save:  ден2,314.27
MOP$806.50
List Price:  MOP$1,129.14
You save:  MOP$322.63
N$1,910.91
List Price:  N$2,675.35
You save:  N$764.44
C$3,692.82
List Price:  C$5,170.09
You save:  C$1,477.27
रु13,360.74
List Price:  रु18,705.58
You save:  रु5,344.83
S/376.13
List Price:  S/526.61
You save:  S/150.47
K381.31
List Price:  K533.85
You save:  K152.54
SAR375.08
List Price:  SAR525.14
You save:  SAR150.05
ZK2,557.08
List Price:  ZK3,580.02
You save:  ZK1,022.93
L466.85
List Price:  L653.61
You save:  L186.76
Kč2,369.96
List Price:  Kč3,318.04
You save:  Kč948.08
Ft37,031.68
List Price:  Ft51,845.84
You save:  Ft14,814.15
SEK kr1,094.85
List Price:  SEK kr1,532.83
You save:  SEK kr437.98
ARS$86,967.11
List Price:  ARS$121,757.44
You save:  ARS$34,790.32
Bs693.40
List Price:  Bs970.79
You save:  Bs277.38
COP$389,858.15
List Price:  COP$545,817.01
You save:  COP$155,958.85
₡50,274.61
List Price:  ₡70,386.47
You save:  ₡20,111.85
L2,477.26
List Price:  L3,468.26
You save:  L991
₲742,412.64
List Price:  ₲1,039,407.40
You save:  ₲296,994.75
$U3,872.48
List Price:  $U5,421.62
You save:  $U1,549.14
zł405.79
List Price:  zł568.12
You save:  zł162.33
Already have an account? Log In

Transcript

Hello, all welcome back to the next one. In the last one, we talked about methods a little bit more. And we saw how we use methods, why they're useful, how we can make use of them to reduce the redundancy and make our code a little bit more smaller, right. Now, let's see and talk about this keyboard called wide. So wide means nothing. And that's the literal meaning up here in the programming as well.

Why means nothing. This key word kind of stands for the return type of the method. Some methods doesn't have anything to return, but some methods do return something. And we'll talk about that. So right now, if we say this method, what does it do? It calculates the grade and then it prints the grade.

It does not return anything. It just prints it on the screen. Even if let's say I just take this out. It's it does not return anything. It just calculates the grade right? returned means that it can return a value from the method after doing some calculation.

And then maybe we can capture that value up here. Why I'm saying up here, because this is the place we are calling the method. So the return value is captured where we call the method. So then this method may return something I'm not talking about, specifically about this method I'm talking about any method can have a return type or not have a return type. If a method doesn't have a return type, then we use wide which means it does not return anything. But if there is a scenario where our use case wants the method to return something, then we can return some value from the method it could be a string, integer or anything else any other data type.

This is the location where we will provide the return type and when I highlighted this void you can see this is the return type of Fine grades method. So we can return something like that. Now let's just kind of try to modify this method to make a use case out of it. And see how does the returning method looks like right? returning means method, which returns something I'm just saying. So let me just create a method.

So let me just cannot divide this. So let's call this method as fine grades, kind of for calculate grades. And let's create one more method for displaying grades. So what I'm going to do here is I'm going to copy this part. And then I'll say display grades. And what should is it x except so maybe it should take same kind of things.

Student name and score. Right. And I got it from that. I want to just copy Yeah, so student name and score. And let's copy the display part, which is the print statement from here to here. And we don't need student name in this method now, do you see that use?

No, we're not using it here. We can take this out. And now this thing instead of score, this thing can be great. Why? Because this guy is calculating grades. Now this guy needs to display grid, so it only needs the name and the grade.

So it's gonna accept two string arguments. One is student name, second is great, or grade. And this method is only going to print something. Just for the sake of doing a little bit more work. What we can do here is we can print some fancy characters, so that we see that the method is indeed doing something right not just printing. So let's see Just put something to kind of neatly displays few things.

And now this method, we want it to return the grade. So what do we want to do here is we want to call this method from the main method. And we want it to return the grade to us. We don't want it to print, we want it to give us the grade back. Now it's up to us, whatever we want to do with the grade, we will do it in the main method. Maybe there's a lot of functionality in our in our application, maybe we want to do some analysis with the student grades and so many other things right.

So in that case, we may not want to print the grade from here we may want to just get the grade from the method and send it to some other method to do some more analysis right. For now, we will send it to the display rates method to print it out. So in that case, what we should do here is we are assigning the grade from Every elsif block if else elsif every block. Finally, we can add a keyword called return. This is the keyword if you want to return something, and grade. So this is our variable, return grade.

And you'll see everything is like kind of error showing up. It says teens method type to string method return type. Why? Because the kind of variable that we are trying to return here is the string type. So we should change the wide to string. If at all we return something of type integer, we can type in here, we can type it here.

So this is gonna be the return type anything that we want to return. Now this gives me error because it returns something. Oh, the first thing is, now it only accept score. So we don't want the student name here. So let's get this out. Let's get this out.

Now it returns a value. But you'll see that we're not catching it, we're not doing anything. If I run this code right now, it's just gonna print it's not, it's not even gonna print the score because we're not calling this thing right. Now to call this method, let's say, if our sake. So let's first find the grade and call the method. So display.

Display grades, it needs the student name, let's say, john, and the grade is it accept something now how do we get the grade I want to give the grade? This method is gonna give me the grade back. How do I get it? So if a method returns something, what we can do here is we can define a variable. So you know what, let me define the variable on the top. So the string grade is the variable name.

And what I can do here is I can capture the grades from this method. So if I do this, the variable equals to the method that we're calling, it's gonna return something, and that return value will be saved in this variable. So now we have a variable defined here, which is going to have the grade. What I can do here is I can just provide the grade here. So I captured the grade from this method, it caught here. And then I provided the grade to display grades method.

And this display grades, it's just for display purpose. It's not returning anything, so we don't want any variable up there. So that's good right. Now for this guy again, I can use the same variable, no need to define an X variable, because first the value will be captured here and will be provided to display great, then the value will be replaced by the new method because we're going to make again, one More call to find rates with this core ad, that's going to provide a new great value to this variable. Now we can again call this method. so here we can say, Tom, is also fine, we don't mind.

So, what we are doing here is we are defining a variable. We're finding the grade, we are displaying the grade. So what we learn here is something about return types. Let me just kind of run it as the Java application. And let's see the output here. So it says grid of the honest a grid of Thomas B.

We kind of decorated it with few stars because we wanted this method to look that it's doing something right. Yeah. But the overall point of context here is that we learn how the return type works, how we can capture the value from the return type, and then use that value for a different method. So now we provided the value we gathered from fine grades to the display great method. And that's very helpful. And one more thing that I would like to say here is like the way we're using this variable here, it's always better to use a variable so don't don't use the hard coded values a lot.

So what we can do here is we can define a student name variable. And for the first instance, we can say student name equals to john and, for this second instance, we can use the same variable equals to Tom. Now we can assign the value here, and in the display grades method we can provide the variable name so now the student name variable has the john value is going to be provided here. Then we will assign the value as Tom is going to provide it here. All good. So we should have the same output when we run this code.

And all good. So creative john is a grade of Thomas P. And we have kind of understood the return type as well. So thanks a lot for attending this one and I'll see you in the next lectures. Thanks a lot, guys.

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.