So continuing on, what we're going to do is use
the 'HelloWorld' application that we
created in the last section.
So I'm just going to click in here, in 'IntelliJ'
and open that up.
And, we're going to make some changes.
So what I'm going to do is make a bit more room on the
screen so we can see what we're doing.
I'm going to get rid of the 'Project'.
The 'Project' section, because we don't need that.
And also, I'm going to run it once.
By clicking on the little green arrow that you saw me click.
And I'm going to make sure we've got some
space here so we can see what we're doing.
So we're going to be typing commands on the left
hand side, and on the right hand side
we're going to be able to see what the results
of running those commands are.
So if your system is compacted like this,
this is a feature of 'IntelliJ';
and if there is only one statement,
it does what's called 'code folding',
and it puts it all onto the one line.
So all you need to do is just click in
somewhere in there, in this case, I'm clicking just
before the word 'System', and I'm going to press
'Enter' or 'Return' on my computer.
And I've now got space to start typing my commands.
So let's start talking about 'variables'.
Now a 'variable' is something that makes
a computer program very useful.
So what is a 'variable'?
Well a variable is a 'place' in memory
think of it like a 'box' if you will
that you can store something in.
And the computer allows you to have hundreds or
thousands of 'boxes', each containing
their own pieces of information.
So what we do with our 'variable',
that's what it's called in Java, that 'box' is
called a 'variable', and what we can
do is we can assign the type of information
that we want to put into the 'boxes'
and we can also give it a name.
So why would we want to use variables?
Well the reason that we want to use them,
is because they make a program really useful.
If, for example, if there was a computer
program that you wanted to create
that helped calculate someone's
current age based on the date,
then of course you'd need to enter
in their birthday, and the computer would need
to store that somewhere, so it could then
calculate how old that person was.
Without the ability to store something,
the computer wouldn't be able to process anything and
the programs you create would be very very
basic and not really useful.
So a variable is used for that
purpose, to store information.
Now as we move forward with the course, we'll be
talking about lots of different types of data.
But we're going to start off with a really basic
one that's included in Java and it's
called the 'int' which is short for 'integer'.
So an integer in math terms, is
a whole number without any decimal points (.).
And, it's the same thing in Java.
So what we need to do if we want to use 'variables',
we need to define what is called the 'data type'.
Then we need to assign a name to it.
And optionally, we need to also define
a 'value' to it. So what we're going to do is I'm going to
go to the end of the line and press 'Enter'.
I'm going to assign a new variable, and I'm going to type
'int' all in lower case,
'int[space] [and I'm going to call it] myFirstNumber
[and I'll put] = 5 [and then I'm going to put]
[a semi-colon (;) on the end of it] ;'
Then once you do that, Java knows that
everything on that line is something that it needs to
process and figure out what you mean.
So the first part of that line,
(and I'll just make a bit more space here)
the first part here, 'int', that's what's called the
data type. So we're telling Java,
please assign a 'box' or an 'area in memory'
to be more precise, that will be able to store an
'integer', an int number, and I'm going to call that
'box' or that 'memory location', 'myFirstNumber'.
And then by using the equals (=)
and a number on the end, what I'm doing is
I'm saying, "I want you to assign the value of
five to it."
Now the good thing about a variable is that
because of it's very nature, it can be changed.
So we can assign a number to it,
and then we can assign another number
to it down the track, and we can continually change it.
But what we've done with this first line, is we've said
at the moment, I want you to assign the value
five to our integer which has got the name
'myFirstNumber'.
And Java has quite happily assigned that.
And what we can do is we can then
print that out if you want too, just to confirm that
that area that 'myFirstNumber' contains
the right amount. I can type
'[System capital 'S'] System [You notice, I made a mistake.]
['IntelliJ' is following along and showing us]
[options below what I'm typing, couldn't find]
[what I meant. So that's a good indication that I typed]
[something wrong. I'm going to press backspace]
[and type] System.out.println()'. Now that's
one way of doing it. I could have gone on
to all that, the entire line like that, but there is
actually abbreviation in 'IntelliJ', and if I type in
'sout'
and you can see what's happened there.
This is actually a template that's built-in to
'IntelliJ', and that's an abbreviation.
So if I now press the 'Tab' key,
'IntelliJ' will print the whole line out for me which is very nice.
So you can see in the first println
at the very top where it's got 'Hello Tim!',
that actually outputted to the right hand side
'Hello Tim'. What would we need to type
in this 'box' where the cursor is in the
two brackets (parentheses) in between the brackets,
to print out 'myFirstNumber'?
So pause the video here and see if you can figure out how to get the system to print that first number.
Come back and press 'Play' once you've done that and see what the solution was. PAUSE NOW - TRY TO SOLVE - THEN PRESS PLAY.
So, how did you go, did you figure it out?
Well to actually get it to print something,
we need to type in the name of the variable.
And we're saying to Java,
"This is the 'box' if you will, or this is the variable
that I want you to print the contents from."
So in this case I just type in 'myFirstNumber'
and I can just press 'Enter' if I can see that
the selection that 'IntelliJ' is showing me is enough
the press 'Enter' and you can see that's
accepted it. There's no errors, and I can click
on over here to the 'green arrow' to 'Run'
So you can see what's happened now is
Java has printed the first statement
the 'Hello Tim!' which was from
previous video when we made that change.
But it's also printed the number five (5).
So that line 'System.out.println(myFirstNumber)',
it didn't actually print the text 'myFirstNumber'.
But rather, what happened was, because we
put 'myFirstNumber' there, the Java knew that
that was a variable. and it went in and
figured out, at how to look at what is in that
memory location, or what was in that
'box' if you will, what was the value in that variable
and it printed out the value not the name.
So if we want back there and made a change,
and made the value '10' there,
and 'Run' the program again;
we actually get the value '10'.
So each time we use that, we
assign that statement if you will,
Java is automatically looking at the variable name,
and looking into the area of memory that
contains the value and printing out the value.
Now, we're not limited to just putting a
a number like that, we can actually put a mathematical
expression there. For example, we could put
10 and use plus (+) the plus sign (+) and say 5.
And you can probably guess what
that answer is going to be.
We'll 'Run' it.
The answer is '15', and that's because
Java looked at the expression 10+5
and said, "well that's 15." It put 15 in
to the variable and saved that into that memory location.
Now these mathematical processes
can get quite complex. They're literally like a
scientific character. We can introduce
brackets (parentheses) if we wanted too. We can put
(10 + 5) + (2 * 10)
So in theory that's 10 + 5 = 15 +
2 * 10 is 20 so it should be
15 + 20, so our answer should be 35 when we run it.
And you can see that the answer we're showing is 35.
Now what do you think would happen
if I copy this line? I'm going to copy
that entire line and we're going to paste it.
What will happen if I do this?
So what I've done is I've taken the variable
name of myFirstNumber
but I've surrounded it in double-quotes (").
And the hint there is that it changed
to 'green'. You know it's much like the first
println with 'Hello Tim!', it changed to 'green'.
What do you think will happen in there?
Do you think it will print out the number 35?
Or will it print out 'myFirstNumber'?
Let's 'Run' it and see what will happen.
And you can see what's happened there is
because we specified double-quotes ("),
we've told Java, "This is not a variable
this is actually a string, and we want you to print
whatever is in there and not to try and decipher that
as a variable, but just to print it out as it was." So this
case it literally, all it did was printed whatever was
in the double-quotes.
So generally speaking, if you see something in
double-quotes ("), that's not a variable
that's going to be treated by the computer
as just a string. As literally whatever you wanted
to type in there. And I could put something else in there,
mySecondNumber. Which obviously
we haven't got a variable called 'mySecondNumber'.
We could 'Run' that.
And it just prints out 'mySecondNumber'.
because there's no relationship to the
variable there and what we're typing in double-quotes.
So let's go ahead and create a second variable
We're going to call it 'int mySecondNumber = 12;'
and I'm going to put 'int myThirdNumber = 6;'
And I'm going to delete that second line there.
And then I"m going to put 'int myTotal =
And I can put 'myFirstNumber + mySecondNumber
+ myThirdNumber
So what's going to happen there is,
the computer comes along and assigns
the 'myFirstNumber' the value, in this case
35, mySecondNumber is assigned the value of 12.
And myThirdNumber is assigned the value of 6.
And 'myTotal' is then assigned the value of
the first number which was 35,
second number which is 12,
third number which is 6.
So if we then go to print out myThirdNumber
and I'm just going to copy and paste that,
We should get the value of 35 for that line,
+ 12 is 47 + 6 is 53. So let's 'Run' that.
And why did it come up with 6?
Well that's because I actually printed out myThirdNumber.
which was the value 6 and not myTotal.
And this is another good thing that 'IntelliJ'
will come up and tell you is if you see a variable
that's grayed out like that,
and if you just hover your mouse over it
it actually gives you a hint and says well if myTotal
is never used, like so. and it says
'Variable 'myTotal' is never used'
So in other words, the computer was
smart enough to figure out, that okay you
set up this variable but it hasn't been used anywhere.
So if I go ahead and copy that,
and paste that into the line to printout,
that isn't 'gray' anymore
because 'IntelliJ' has quite correctly figured out
okay, you're now using that. So now if we 'Run' this,
we should get the value of '53'.
So as you can see it really can be quite complex.
One last thing I'm going to do is change this a little bit.
And you can even do something like
myThirdNumber = myFirstNumber * 2
So therefore the value would be 35 for the first line
12 for the second line
but then myThirdNumber is the first lines value which is
we saw was 35 * 2.
So that line would be 70.
So it would be 35 + 12 is 47 + 70
is 117. So if we now 'Run' this we should get the value of
a 117.
And you can see we've got a value of 117.
So let's end with a challenge.
So how would you define a new variable
that had a difference between 1,000
and the current value of 'myTotal'?
So in other words, how would you
assign that variable that it figured out
that whatever the total was, whatever the
value in 'myTotal' was to take that
from a 1,000 and to show the value from that and then to
print that out on the screen. Go ahead
do that. Pause the video here and then come back when your ready and we'll have a look at the solution. PAUSE NOW - TRY TO SOLVE - THEN PRESS PLAY.
So how did it go? Did you figure it out?
Well to actually do that,
you can type a new variable. You can type
'int myLastOne = [and it's simply a matter of putting] 1,000 - myTotal;'
So that will then look at the number 1,000
deduct from it 'myTotal'
and leave us a value which in this case should
be the difference between a 1,000 and 117
which is 883 if my math is correct.
So what we can do is we can actually change this now.
Paste that in there, and if you 'Run' that,
We'll actually 'Run' it.
And there we go, that's the value of 883.
Now just a note before I finish this video,
case sensitivity is very important here.
Now I actually made a typo there
Generally speaking, when you're creating new
Java variables, they should start with a lowercase 'm'.
Now the point I want to show you here is
by changing this variable name,
to what it should be which is starting with a lowercase
number letter you now get an error and that's
because you typed it in uppercase.
And that's because Java is case sensitive when it
comes to typing your commands.
That's very much a common source of problems
when you're learning a new language is to get
the syntax wrong. In other words to type a word wrong.
But also not type it in the right case.
So as you can see there, because I've changed this
to a lowercase 'm' now from a human perspective
that's so obvious, well okay, isn't that the same
thing, because clearly it says 'MyLastOne'?
No, it's not the same thing as far as the computer's
concerned, because the 'm' here is an uppercase 'M'
and that's a lowercase 'm'. So we need to go back
and change that to a lowercase 'm'
and by doing that, that fixes the error.
So do make sure you check that out.
So it's a very common cause of errors.
And if you see something like that highlighted in 'red',
that's usually a good indication
there's an error with your typing,
and you can hover your mouse over it,
and it will usually tell you what the error is.
In this case 'Cannot resolve symbol 'MyLastOne'.
And what that's saying is it cannot find
a symbol or actually a variable in this case
called 'MyLastOne'. We'll change it to lowercase 'm'
It now matches the variable name that was assigned on
previous line and it will work.
So that's the 'integer' data type.
We've now learnt the basics about that.
In the next video we're going to start looking at some
other data types, and how to mix and match those
and take our results to the next level.
So, I'll see you in that next video.