Java Programming Tutorial 8 - Variable Declaration and Initialization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo yo what's up everyone this video we're gonna be talking a little bit more about variables and how to work with them specifically assignment initialization all that good stuff so before we get into all that we need to give credit where credit is due and with that I wanted to give special thank you to our sponsor pram pram has been tremendously helpful to this channel sponsoring my content and it's always good to see companies give back to the development community so if you guys don't know what per amp is it's a peer-to-peer web app where you can go on there you get paired with another individual and you practice interviewing one another so you interview them and then they interview you and then they give feedback on how your interview went and you can use that to improve your interviewing skills so not only do you get to practice these different problems that they give you like data structures and algorithms front-end design system design but you also get that feedback from other people so you can start improving where you're weak so if you ever want to succeed as a Java developer or a software engineer you definitely need to check out pram when you decide to check out pram be sure you use the link in the description as that will really help my channel out as well alright let's get started so we have been working with variables and we're understanding the basics for the most part but now I just want to go a little bit more in depth on how we create variables and just the different options so I'm just going to clear out all this junk okay so we can create a variable like this in X equals five we just created a primitive type variable because it's of type int and there's eight primitives we talked about those in the previous video well this is actually a two-step process and we're just doing them both at once so here is how we create a variable step one is we declare a variable and then step two is initialize a variable so when we do this we're actually doing both at once but we could break this out into two steps and that's what we're gonna do now so if I go in here and I said int X and just put a semicolon that would be one statement and then we would say X equals five that would be a second statement so yeah that's all I got for you guys no I'm just kidding we're going a little bit more depth so why would you ever want to do this why would you declare variable on one law and then initialize it on the other well you might be in the situation where you're thinking and saying oh I really need this variable X but I don't exactly know what the value is going to be right now so what we're gonna do is we're just going to declare it up here and then later on when we get the value that we need we can assign to it using us an initialization and what that'll do is if we if we create X in the right spot well then X will be available throughout our program and that'll make more sense once we start talking about variable scope but it's really super important where you declare your variables because where you declare your variables determines where you can access and see these variables so later on when we talk about like if statements if you define a variable int X inside of here where are these these curly braces right here well now we're gonna get an error because X is not defined outside of these curly braces so you might be in a situation where hey you know you're going to need the variable X inside of all these different scopes so we might declare that variable outside and then inside we can assign to the variable and that means we could go down here and we could still reference the variable X so in here it's assigned the value 5 and then we reassign it a value 10 so that wouldn't be possible if we define the variable inside of here so if we did it like that well now this is gonna be an issue because X isn't defined out here once again so all of the rules on where to define variables that'll be cleared up once we talk about blocks and variable scope but for now just know that sometimes it's important to declare the variable as a separate statement than initializing the variable and here's another thing if you declare a variable on one line and then assign it a value this is known as the initialization but what we can actually do is we can actually re assign values to this variable so like I said an in the preview example we can say X equals 10 so when we do this we're not creating a new X it's always referring to the same variable X and this simple thing is important because once we get into some more complex operators and stuff there's things we can do with the variable X that will basically reassign values to X or change the values of X basically what I'm trying to say is that X can be changed it's not constricted to just storing one value and never changing that's because it's a variable huh see what I'm saying the variable because it changes you know I'm saying okay the other thing you can do is a constant now if you want to make a constant you can prefects the declaration with the keyword final so we can say final int and then just by convention you can name it just like any other variable but by convention when we create a constant we use capital letters so we could say final in y equals five and now if we go down here and say y equals ten we should get an error and it says y cannot be assigned obviously because it's a constant so this can come in handy if you want to create a value that you know is never going to change like PI for example so you could go in here and create a variable pi and you can go in here and give it a value like 3.14159 and this will actually not be an integer because you can't have anything with a decimal point so if you wanted to do that you could change this to double or float we'll get into those data types soon when you just get rid of this line here now sometimes you'll see variables like this created outside of this main method like this and you also see some differences you might see public static final for example and basically we're just creating a global variable which by the way I'm not saying this is recommended I'm just saying it's possible and you often see this we're just creating a global variable PI that has a value that can be used throughout the program static once again means we don't have to create an instance of this class then if you wanted to use this variable PI inside of here you could just use it like that but if you're actually inside of a different class later on you could say my sweet program dot pi and that'll give you the value 3.14159 and obviously this is giving an error right now because we're not doing anything with it but just to show you that it works you could assign it to it they're variable like so so now the air goes away and the reason inside of another class you could do this is because it's labeled public which means any class can access this member PI all right so now I briefly mentioned whether or not this was a good practice my rules that I generally try to follow for myself is that if I'm gonna have a global variable I'll try to make that thing final so that way it's not getting changed throughout different areas of my program so I try to keep things final meaning that the value can't be changed just for things like PI that aren't going to change or what I'll do is I'll actually make this private and then I can make what's known as a getter and setter for this variable to basically restrict how this this value can be changed so to summarize you can declare a variable and you can initialize the variable Wow I probably could have made this video like 30 seconds long so if you've enjoyed this content please consider subscribing as well as checking out the links in the description for the Java crash course the blogs and the sponsor of course thank you guys I'll see you in the next one [Music]
Info
Channel: Caleb Curry
Views: 72,412
Rating: 4.957015 out of 5
Keywords: java programming for beginners, beginner, tutorial, java, tutorial 8, variable, declatation, initialization, caleb, curry, calebthevideomaker2, how to, code, coding
Id: 1mRN2MwdWUo
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Tue Nov 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.