Beginner Java Tutorial #4 Declaring & Initializing Variables, Displaying Text

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is Shane from develop Java comm exam bringing your fourth Java tutorial and this tutorial today we're going to be talking about declaring and initializing variables as well as talk a little bit about methods and also show you guys how to display some content on the screen we'll go ahead and build our first program today so we have this test class that we created before remember like I said before every time you create a new project you automatically have what everything in the project you're going to end up with a class file which is this right here and in this class file we've made our first class which is tests and like I said last tutorial is every part of Java every part of the code that you write always has to be within a class so we have this test class here and these curly brackets are showing us everything that we have in this class right now there's nothing but we're going to put some stuff in this class so let's go ahead and hit enter a couple times and move that second bracket down so now we're saying everything between this first curly brace and the second one is going to be everything that's in this class that we're using now the second thing we have to do here to write a program is put in our main method now don't don't get discouraged or confused by what I'm going to do here there's getting some stuff you're not going to understand but that's okay just bear with me just try to remember memorize sorry memorize this code that I'm going to put in and then later tutorials you'll understand what it all means so we're going to do here is we're going to do public static void main we'll do opening parenthesis right in string with the capital S to open and close brackets space arg s and then we're going to open and close to curly braces and really curly brace down so what we've done here now this is called a method we've created a main method now methods in Java are places where you usually do like calculations or output information on the screen it's kind of where all the nuts and bolts take effect so whenever you you're in a method in then the program runs the method that's what things kind of that's where the thing happens and one important thing to know about this main method is this is where Java starts the program so anytime you have a job java application and you tell it to run that application the first thing it's going to do is look for this main method and that's where it's going to start running first and from there it just goes line by line and starts running every single piece of code that you have in that main method so the first thing we're going to do here is go ahead and create a couple variables and if you remember from our last tour we talked about data types every variable has to have a data type we have to tell Java what type of information we're storing in that variable the information we're going to store today is going to be the name of like two or three of your family members along with all their ages so the first thing we're going to do is we're going to store the names so that's going to be of type string if you remember from last time so we write the word string with a capital S then space and then our variable name so for this I'm just going to use my family's name so first I'll do you mean with just Shane and then I'm going to put in equals and then I'm going to put my name in the double brackets member anytime we have a string variable we always use those double quotes sorry not brackets quotes I'll put my name there and as far as this you need to use a semicolon after this and it's kind of just going to be a learning process for you as you go through when to use a semicolon when do not normally after every line of code you're always going to want a semicolon when you're enclosing a class and a method those use curly braces but anything within those is usually has a semicolon afterwards so the next person will do here will do my wife so string her name is Jana and we'll put her name in here all right and let's you know let's change these variables make a little easier eyes I'm just going to put me and then I'll put life so we've done here is we've told Java anytime you see me me equals Shane so any time you see me Mira looming chainsaw also doing a storing chain in the variable me and we're telling Java that the variable the information the variable is a type of string and same thing here any time Java seats wife in noses Jana and we just told Java that that that information is type string so it's going to add a semicolon after that and now we're going to go ahead and do ages so now agents age is going to be a number it's going to be a different datatype remember whole numbers always integer and they're they're displayed as int in Java so this is going to be int variable and I'll put me age and then my age is 26 put a semicolon after that and we'll do another one here for my wife int will do wife age equals 26 as well and our semicolon and now if you see the variables I've created here you notice this variable has two words so in Java it's it's kind of common practice when you're programming Java to do what's called camel casing and what that is is the first word of a variable name is always lowercase any additional word after that is uppercase so if we had it add another word on here like say today that's going to be uppercase as well so only the first word is lowercase every additional word after that is uppercase and it just helps you to be able when you're going through variables to be able to read them a little bit easier all right so you may notice here with this little yellow lines under each of these and if you hover over it a bunch of stuff comes up but like when the world's going on here all this means are here and if you look over here and hover over this it's a little bit of an easier explanation it says the local variable me is never read so that means is we've declared this here we've declared a variable we've initialized it to the value but we haven't used it anywhere so that's all it's telling us right now this isn't a critical error it's just saying hey you haven't used this yet just be aware you need to use it and that's what we're going to do right now so again this is one of those things where you may not understand completely what I'm typing here but just just realize what it's for and try to memorize the the syntax or the wording of so we knew now is we're going to display some information on our screen here so first going to do is when Isis tum' the capital s dot out dot print PR I in T and we're going to put open and close parentheses after that just don't worry about the arg0 there in the middle delete that out we're going to put a semicolon after that so what we've done here is we've pulled Java that on our system basically on our computer we want to output some information to our user and what we're going to do that we're going to print information on our screen so so on our system our our computer we're going to print information out to the user essentially what that's saying here and now any of the information that we want to put out to the user is what's going to go in these parentheses here so let's say let's just put on let's use my name and my age that's what we want to put out to the user so we're going to put me because me equals chain and then we're going to put me age because that's 26 and so that should print out on the screen for us it's going to put my name and my age on the screen all right so if you see this here this red underneath this what this is telling us is it's a syntax error because saying hey something the way you type something in or something's not quite right here so you need to fix this now the problem here is that I have a space in between this now anything in here it doesn't recognize a space any any characters anything you type out on the screen either has to be in a variable or it has to be of a type of a string that's in parentheses so we're going to do here is we're actually going to add a space in here to do that we're just going to use a double quote then actually add a space another double quote now this right here still won't work because we got to tell it hey we want to display me do we want to display the space and we want to splay my age well Java doesn't really know what to do with this the way we've written it so we have to do is we have to what's called concatenate these and concatenate means it's just going to put words together so to do that you add a pause in between each thing so we'll add that here and that there and look at all of our airs ago so I'm saying is saying display me which is Shane Plus display this space Plus display me age which is 26 so that's what should we should get on the screen when we run this we should get Shane Space 26 so let's have an give it a shot let's see what we get so to run our program since we don't have any here as I said the yellow ones we don't want to worry about too much just because they're saying we haven't used these yet and that's fine we know we have it but any read errors that we get those are the ones you need to worry about you to fix those before you run the program but this button right here will run it for us so it's going to click that let's see what happens all right just coming up and it's asking what do we run around we wanna run this test that's right so let's hit OK and you can see right down here this is where output displays and we got exactly what we thought we would we got Shane spaced 26 exactly so let's go ahead and add in my wife's name and my wife's age here so again since we're combining things here we want to go and add the plus concatenate these together so plus we'll add another space and we'll do a plus do life plus do another space plus life age and so we should get now we should get Shane space 26 Jana space 26 so let's run this again and let's do it again yes that's the one we want to run and that's exactly we cache in space 26 Jana space 26 so I hope this made sense for you guys this is our first tutorial here and like I said we'll kind of be building on this I'm in the next tutorials and we doing stuff a little bit more complex and explaining all this stuff to you guys as well so we went over today we went over declaring and initializing our variables like we did right here and we talked about our main method how when Java runs the program this is the first thing it looks for and it runs everything within these curly braces first and then we also displayed information on screen so be guys enjoyed it if you like my tutorials make sure to subscribe to my channel so you can get updated whenever I get new tutorials out and as always check out my website at develop Java calm Thanks
Info
Channel: Freetechtorials
Views: 40,683
Rating: 4.9480519 out of 5
Keywords: Beginner, Java, Develop, in, Program, Learn, Tutorials, Videos, Programming, Coding, Writing
Id: W1GEMdHnCQE
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Sun Jul 15 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.