First Step – Variables

First Step – Variables

Articles Blog


In the last few videos we saw how to create our first programme in Java. It was pretty basic to say the least printing out some text on the screen but it was a good place to start. But if we wouldn’t start getting the computer to do something useful, we need to start using variables. So what are variables? Well, variables are a way to store information in our computer. Variables that we define in a programme can be accessed by a name we give them, and the computer does the hard work of figuring out where they get stored in the computers Random Access Memory or RAM. Now a variable as the name suggests can be changed, in other words its contents are variable. So what we have to do is tell the computer what type of information we wanna store in the variable and then give the variable a name. There are lots of different types of data that we can define for our variables. Collectively these are known as data types. As you may have guessed data types are key words in Java. So let’s start out by defining a variable of type int, int being an abbreviation for integer which was a whole number, that is a number without any decimal points. To define a variable we need to specify the data type then give our variable a name and optionally at an expression to initialise the variable with a value. And we’ll talk more about expression shortly. For now though, let’s swing back to the code to see how to define our first variable. so I’m gonna start below the printlin line, press Enter there and just put a bit of a gap there so we can see what we’re typing. I’m going to type int my first number noting that I’m capitalising the word first and the word number, equals five. I’m putting a semicolon on the end of that line to complete the statement. So here we’ve defined our first variable. We’ve specified a data type of int. Again that was an integer, a whole number and we used the equals operator in the literal value five to set the variable to have the value of Five. And finally we added that semicolon to end the line and a former statement. So the semicolon ends the line and tells Java, just that that the lines complete. So this entire line as we’ve talked is a statement or to be more specific it’s a declaration statement. So a declaration statement is used to define a variable by indicating the data type, and the name, then optionally to set the variable to a certain value. So here the type, and type is a shortcut for the word data type is an int, the name is my first number and the value were assigning or initialising our variable with is five. So we’re declaring a variable of type int with the name my first number and assigning the value of five to it. The initialization is optional, in other words we could have admitted the equals five with Java in general variables have to be initialised before being used, but we’ll talk more about that in an upcoming video. If you are initialising a number, what you type to the right of the equal sign is assigned as a value to the variable. This is known as part after the equal sign as an expression in Java. And we will be using expressions a lot as we go through the course. So what is an expression? What’s a construct that evaluates to a single value? And we won’t go into a deep discussion of that now because we’ve got upcoming videos where we’ll talk about that in a lot more detail. Now Java has or will when we compile and run the programme, read this statement we’ve created and allocate a place in memory to store a single whole number, and we’ll assign the my first name to that area of memory or more specifically assign that name that we’ve talked to my first name to that area of memory. So that’s how we’ll access it. So in other words you don’t need to know anything about where in memory this is taking place or where Java is storing the contents. To get access to it you don’t refer to a memory location, you refer to a variable name, my first number in this case. We’re leaving Java to do all the dirty work there. All right, so now that we’ve declared the variable, let’s see if we can print out the value. So looking at the declaration statement I think we’ve got a good idea what the value should be output. So the challenge is to look at creating a new system to that top printlin below the declaration for my first number, that variable, and see if you can figure out how to print out the value my first number. So pause the video and have a think about it, and try to get to print out the value that we assigned to our variable. Resume the video when you’re ready to see how to do it. Pause the video now and I’ll see you when you get back. Welcome back. So you may have thought, when you first started that this would be how to do it. I’m gonna give you a typical example. Now what I’m going to do is top this out. This is the abbreviation in intelligent, if I press tab there, note that tops hand only tops in that entire line system.out.println, and that’s a very common thing that we’ll be typing in this course. It’s good to get used to using those shortcuts. SOUT in tap. Right, so when you first try this, you might have tried putting double quotes in the parenthesis similar to what we did on line four and top my first number. I worked on that and then when you run the programme thinking you get the value, you know, my first number variable. When you run it you saw we in fact got the output my first number and not the contents of our variable that we declared on line six, which of course the value should be five. Just close that window down. Now the reason that we’ve got my first or had my first number printed, and I just run that again actually publish until closed in window. So the reason we’ve got my first number printing out and not the contents of our variable is because we put the text in double quotes my first number. And when we put something in double quotes is called a string literal. Now a literal and like a variable cannot be changed. It’s an expression and not a variable. So here what we’re saying is literally no pun intended print out the text, my first number. And unsurprisingly perhaps, then after that we can see my first number showing. So in essence that’s why we’re getting that value. But if we actually remove the double quotes, which I’ll do now. We remove both the ones at the start, and I think we’ve now got an error on the end. So we need to remove them both to remove any errors, and we run this again. This time we should see the right value. And we can see the value five, you’re showing on the screen. So I made sure that I topped the name identically to the declaration line, on line number six, and you can see we got the value five showing. So just to confirm that if we changed the fivE to attend and run this, we’ve now got the output showing us number 10. Now the expression to the right of the equal sign can be a lot more complex. At the moment we’ve just used a literal integer value on number five, and then you saw as we changed it to number 10, but we can get a bit more complex there. So let’s add an expression that is the sum of two numbers. So we will change 10, I’m gonna to add a plus and a number five there. It’s about an operator and then the number five, so it’s 10 plus five. So run the programme now. And we’ve now got the value 15 output on the screen. So Java has looked at the expression to the right of the equal sign, and figured that that is a mathematical expression, and it’s basically calculated, that to be 10 plus five equals 15. And sure enough we get that value assigned it to our map my first number variable, then line 7 prints that out to the screen. Now we can get a lot more complex, let’s do that just before we finish the video. So we can change this to something like say, let’s put parentheses around the 10 plus five, and then we’ll add a plus in another set of parentheses, and then now we’re gonna put two multiplied by 10. That should give us 35, 10 plus five, that’s 15, plus two times 10, the two times 10 is 20, That’s 15 plus 20 is 35, let’s run that. And sure enough we get the value 35 showing. So you saw that I introduced an operator there, recorded an operator. In fact we’ve got a number of operators there. We’ve got the plus operator and the multiplication operator showing. So Java operators or just operators perform an operation against the word on a variable or value. Plus, minus, divide and the multiplication of four common ones that I feel sure you familiar with, but there are lots more operators, and you’ll be seeing those as we go through the course. By the way if you wanna get a downloadable list of all these slides navigate to the last section of the course and look for a lecture called downloading slides. All right, so let’s end the video here and in the next one we’ll figure out how to use other variables in expressions. See you in the next video.

Leave a Reply

Your email address will not be published. Required fields are marked *