Java Programming Tutorial 4 - Arguments and Parameters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo yo what's up everyone this video we're gonna be talking about this right here as well as this here so basically in the previous video we talked about the foundation you know like what in the crap is going on with all this code here but now we're gonna be talking about arguments and this is an example of an argument before we get started in this video I wanted to give a special thank you to our sponsor pram pram is an awesome website where you can get paired with other developers and practice interviews to get a job in the industry so they have interviews for data structures and algorithms where you can choose the language that you want to use to solve these algorithm problems but they also have interviews in different topics such as system design front end and even behavioral interviews and data science so if any of those topics interest you please be sure to check out the link in the description ramp is the one thing that will take your development skills to the next level and really validate your knowledge and help you get a job at places like Google Twitter and so forth so anytime we call a method which is if you guys remember a method does something and we have these parentheses when we put something inside of these parentheses this is an example of an argument so hello there is an argument and this main function that we're defining here this main method sorry is expecting an argument that is of type string array and we're gonna talk about how we can use this so what we're gonna do is we are going to in the package Explorer right click our file and we're gonna click show in and then say system Explorer alright so this is gonna open that Java file in a in a window and then what we're gonna do is we're going to basically back up a couple of files so if you don't see this icon right here what I want you to do is customize toolbar by right-clicking and then find the path and just drag that down drag it up here to right here okay cool once you got that I want you to back up to the project folder so the hello project is right here and the package Explorer and it's right here in our in our file system so there's two folders here that you'll see the source folder and the bin folder the source folder contains our source code the bin folder contains our object code our compiled code so what we're gonna do we're going to open a terminal and here's what we're gonna do we're going to basically in the terminal change to that directory by using the CD command and then I want you to basically drag that bin folder to the directory and it'll type out the location of that and then press Enter so now when we say LS we should be in the same folder as my sweet program dot class all right now what we can do is we can run this application from the terminal by saying Java and then just saying my sweet program but you don't put the dot class you just leave it my sweet program and press ENTER and you can see the code executes so that is how you run a Java program from the terminal so that's pretty cool but now what we can actually do is we can make our application a little bit more dynamic by passing some extra info in there for example what if we wanted our application to say hello Caleb well we could actually come in here and add a another argument and say hey my name is Caleb oops can't spell my own name right but you can see when we do that nothing happens it just says hello there so what in the world is going on well the reason nothing's happening is because we're passing in an argument to our program but the program's not actually doing anything with it so if we wanted to actually say hello Caleb what we need to do is we could actually go in here and say hello and then put a plus sign and then say args of 0 just like that now when we run this in the terminal you can see it says hello Caleb so that's pretty cool we basically just created our first dynamic program and this basically is the foundation of all programs our goal is to not make applications that do the same thing every stinkin time what we want to do is we want to make an application that's interactive you know it says our name it allows us to type in things and do different things depending on what we want we want to make our application dying so what we just did here of passing an argument with our programming name it's actually not super common and in the upcoming videos we're going to be talking about how to do different things like asking the user their name and then we don't have to worry about passing in the user name right here we can just call the program and it's gonna work just as expected because right now if we don't put that in there we're gonna get in there Oh exception and thread main by the way guys this is an example of a run time error so we were able to run the program but what happens is that the program crashes so we didn't get a compiling error like we did over here in this example for example if we go in here and we type something wrong and we try to run this thing well actually I'll go back to the terminal here if we try to run this here well now we get a compiling error those are two different things compiling errors are good because they allow us to fix our code before it runs runtime errors are bad because they crash our program and we don't always catch them before it's too late so I'm gonna go back in here and fix this I'm gonna save I'm gonna go back in here and run our program like normal so you understand how to use a command-line argument but just so you guys really understand what's going on here when we say Java my sweet program we are basically saying yo dog we want to run the main method of this class my sweet program so it goes in here it looks for that that method oh here it is main and here's what we're gonna be doing in that method and then this thing right here string brackets args this is what it's expecting as arguments and it doesn't have to take any but we can pass arguments so we could pass multiple arguments we could say Caleb and I don't know Sally and we're not using that second one here so it doesn't really matter but in this situation we're using that first argument and the way we get the first argument is with this bracket 0 and this is a race tax and don't get overwhelmed with that if you've never used a raise because we're going to be talking about that the main thing you guys need to understand from this video is what an argument is and how to use command-line arguments arguments are going to be used all throughout coding so it's really important to understand anytime we call a method such as this print line method well we're passing in an argument or passing this entire expression here so we get hello plus whatever the the first array element in this args is so that's another example of using arguments in our code so another thing you might want to add in here is arguments and this is what you pass to a method now another term you're going to hear a lot and I got to get rid of this error it's driving me crazy I'm just gonna run this so you can see we get an error here and that's because we didn't pass in a command-line argument because when we hit this play button we're not passing any arguments and so I'm just gonna go back and just delete this all right so now I should run okay now what I was saying another term you're going to hear a lot is parameter and what is a parameter a parameter and an argument are very similar but they're not the same thing you'll often hear them used interchangeably but I would say if you want to be a really good developer you'll definitely want to understand the difference so an argument as we learned is what you pass in to a method so we're passing in hello to the print line method a parameter on the other hand when we define a method is the variable that's going to store that data so for example this main method we're saying hey we want to store the arguments in this right here a string array called args so this thing here is expecting an argument but what this thing is known as is a parameter and the distinction between those two will become clear once we start defining methods but basically we're defining this main method here because we're doing all this prefects stuff but we're just calling this print line here we're not defining it it's already defined somewhere so when we define a method we give it when we call a method we give it arguments so these are variables to store arguments they're part of the definition of a method and the arguments are part of the calling when we say calling all we mean is we're saying the name and putting these parentheses which is going to execute the code inside of this method using the argument hello now oftentimes you're going to be using other methods that have been created and you're going to need to know what kind of arguments they need so when you call a method so let's say we wanted to print something else and we say print line you can see there's all of these different signatures here the signature is basically just the structure of the method and you can see there's a lot of different options so we don't just have one print line we basically have an overloaded version with a bunch of different options for arguments one of them takes nothing one of them takes a boolean one of them takes a character a character array double and so forth the one we've been using is print line string and that's because Hello is an example of a string and we're going to get into all these data types coming up soon but just so you know if you're not sure what kind of arguments the the method takes all you got to do is just type it back out and it will just give you some some hints so you put the dot and then the function name and then you can go through here and get details about all the different versions so that's pretty much everything you need to know about arguments and parameters for right now I would even go so far to say that if you understand everything we've talked about up until this point you are far beyond what most people understand in development what we're going to do now is are basically going to take a step back from all this deep-diving of the concepts and we're just going to go back and learn the language fundamentals so all of the things we've previewed and talked about we're gonna go in all those in more depth so you can start understanding how to build more complex applications if anything from the last word videos has been challenging trust me as you go on in these next videos it'll all start to clear up so don't worry about having a perfect understanding of everything with that I'll see you guys in the next video and before you go just please be sure to check out the description for a link to the blog the Java crash course as well as our link you are amazing sponsor pram who will drastically change your life thank you guys I'll see you the next one [Music]
Info
Channel: Caleb Curry
Views: 170,525
Rating: 4.888268 out of 5
Keywords: java programming, java, tutorial 4, tutorial, how to, arguments, parameters, java tutorial, java (programming language), programming, code, coding, beginner, java programming for beginners, java for beginners, caleb, curry, calebthevideomaker2, java programming tutorial, java programming projects
Id: U5b9TH14REM
Channel Id: undefined
Length: 11min 30sec (690 seconds)
Published: Mon Nov 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.