Return Statement in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you just started making methods and you're probably looking at return statements and if you're anything like me you're probably confused as hell but I'm going to show you exactly how to use return statements in Java and by the end of this video if you watch all the way through you'll have a fully working program using methods with return statements and it's gonna be great hey what's up it's alex back again helping you learn java on this channel I make Java tutorials for beginners every single week so if you're new here that's something you're into then consider subscribing the very first thing we'll do is hop into eclipse we'll click on file new Java project we'll call it something like return statements okay next go into your newly created project right click on the source folder and go to new class this is our Java file we'll just name it something like return statements again click this first check mark and hit finish you are all set up to start working a return statement happens inside of a method so I'm just going to make a method real quick as an overview we have this class called return statements the class is another name for a Java file and it'll have a bunch of these methods inside these curly braces it already gave us one method and that's called the main method this code gets a run once you click the Run button to make our own method we'll just copy this little template for now we'll say public static void we'll name it something like print a message like that and we'll do our open and close parenthesis and then curly braces and just like how when the main method gets run with the green run-button this code inside the curly braces gets run we want to put some code inside of our customized method and we'll just do that we'll print out something like this is our first method okay save that and run it we see nothing gets printed out because only what's in the main method gets run so we can call our print a message by just typing it since it lives in the file we can refer to it by this name print a message so I'll just type that print a message and then the parentheses semicolon save it run it and we get that code because when we click run we go in here and then we go here and then we see that this is this and this has this code now on to return statements let's make another method called add let's add some numbers so we'll just do what we did before these words we're not I haven't really gone over too much about what these keywords are but we'll just keep moving on we'll call it add and we want it to add two numbers so we can pass two numbers into our method by putting them in these parentheses so you want to add some integers we can just do int a and say int B we can of course print out the result a plus B save it and then call that method with a few numbers in here it will say like five and four this is how you would call a method you wrote with two parameters save it and run it and you get nine printed out but what if we want to store that value into another integer say sum we can't really do that um if we wanted to say like in sum equals the results of adding five and four you can't do that but now come return statements return statements let you run a method and then bring the results of that method into a variable you want and that's why we have these red underlines because we haven't set up return statements yet this third keyword public static void this keyword can change and it's actually the type that the method will bring back to you this void just means nothing it means once you call the method it runs the code and that's it it goes to the next line it doesn't store anything into this code so when we try to assign our integer sum equals add 5 & 4 it's saying well what the heck this is void this is nothing I can't use this so to use return statements in java if we want to compute a plus b and then have the results of a plus b stored back into the method call itself we would replace void with the type that we want so it can be int have some red underlines this says add a return statement you could just click this like this or you know you could just type it yourself I'm going to modify it a little bit will return a plus B and now our red underline up here went away I'm gonna print out the sum and then I'll go over everything step by step so that you don't miss anything and you fully understand what's going on we click the green run-button we go into our main method the first thing is print a message since this method print a message is in our Java file we know what it does and it does this code between the curly braces so that's what gets run first next we go to the next line and we say we want to declare an integer variable and call it sum and set that equal to the result of another method we called add which adds two numbers together so we go to add right here we pass 5 into a and 4 into B now we run this code and we hit the return statement before we do anything with the return statement we need to look at the code after it a plus B so we do 5 plus 4 and we have 9 right here return 9 this takes 9 and puts it back into the original method call this now is the exact same thing as nine to the computer if this was eight we'd returned five plus eight and this would be thirteen and since now we're returning thirteen and this third keyword let's it know what typed the returnees it's an integer since this is an integer thirteen and this is an integer we can now set sum equal to the results of ad which again is the integer thirteen brought back by the return statement and lastly we're printing out that some will save it and run it and you'll see the first method and then we're printing out thirteen so now we know two return types we can return nothing and just run this code with void or we can do something and return a variable say the type of the variable and do it that way now let's do another return statement example in Java by making another method and saying to capitalize some word or sentence so we'll make another one public static and then the third one is the type and this is going to be of type string since we're working with words so we type string here and I'll name it like caps and then do our curly braces right now there's a red underline because it says this method must return a result of type string so we can click add return statement or you could just type return and then something like an empty string for now I'm gonna delete these other methods here and just start fresh let's use our caps method by doing string you'll say shouting cuz like you're shouting why are you typing in all caps to me are you mad at me and we'll set it equal to caps we'll call our caps method through here but if you notice we're not really doing anything but let's pass anis string and then convert all those to uppercase like it's supposed to so we can pass in a string parameter we'll just call it s and then we can do things to strings so I'll just type s and then a dot since it knows that s is of type string this dot will let us see all the things we can do to this string and luckily for us there's one called to uppercase which converts it to uppercase now this is right underlined because we have to put a string in here now to match this method why argue something like that and we'll just print out shout it okay save it and run it and you see all of it converted to uppercase I'll walk through the return statement here again we click the green run-button we go into our main method the first line here we're going to run looks like we want to make a string variable called the shouting and we'll set it equal to this this happens to be the name of a method we wrote so we go into that method and set the string s equal to this why are you reading my diary mom we hit the return statement we take why are you reading my diary mom and convert it to uppercase using a string method if you want to learn more about string methods I have done the screen now you can check that out but after we convert it to uppercase we return the uppercase string back here and now it's stored into shouting as we do a final check this shouting is a string and our method returns type string so they both match so that's good there's no errors finally we print it out and there it is lastly I'm going to do one more where we return an array of integers and this sounds crazy like what the but it's literally the exact same thing we've been doing so uhm let's make a method that turns integers into an array of integers so I'll say public static we want to return an array of integers and you can do that by do intz and then the two square brackets this is the type that's gonna be returned next I'll just call it gimme array from Yunus and we'll pass in like like three integers the same a it's B and a and C do our curly braces to see what kind of code we're gonna put in here of course we get this red underline because it says we need a return statement that matches this type integer array I'm gonna hold off for now we'll just quickly declare an integer array call it array equals new int square brackets and then the size is going to be three integers long since we have a B and C it's three the first integer in the array is going to be a so we'll go to the first element in the array which is zero it's tricky you always start at zero not one so to do that we go into our integer array called array at the very beginning at zero and we set the equal to a next we go into the second position one and set that equal to B and finally oh that should be just one array since we named it array will go into the last position position 2 which is actually number three and we'll say that's equal to C and finally finally we can use a return statement return to return something of type integer array and it looks like we have one here array boom we are done now all we have to do is use it we'll say I'm an integer array up here called awesome array equals then we'll call our method gimmie array from intz we'll pass three numbers here I'll say three seven one this will get run it'll return our ray of integers back into this code and then that'll get stored into awesome array finally we'll just print out some values of awesome array so we'll go get the first one copy and paste here and we'll get that position 1 & 2 as well save it and run it and you see each value of the array is returned to us through this method so to sum up a return statement lets you call a method and then store a value into that call and from that value you can store it into things like strings integers integer arrays or you could not do anything with it at all and that's what's called void all the code will be done below if you have questions please ask in the comments you could be anywhere in the world that you're here watching this video with me learning how to code and I appreciate that so much I'll catch you in the next video [Music]
Info
Channel: Alex Lee
Views: 121,356
Rating: 4.9772725 out of 5
Keywords: return statement in java, java return, return java, return statements in java, return statement java, how to use return statement, return statements, how to use return statements in java, return statement example in java, return statements java, java return statement, how to use return in java, return statement, return statement example, how to write java code, java, alex lee, java (programming language), java method return, java videos, java (software)
Id: L1htVG3xP5Y
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Thu Mar 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.