Complete Java masterclass for beginners -04 (Variables)
In this part of the course, we are going to talk about variables. 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 we are creating a new project. Now click on
file>new>project.
Click on next.
This time we will check the create project from template
option
Give the project its name
Your project will look like this
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.
public class Main {
public static void main(String[] args) {
int myFirstNumber = 5;
}
}
public static void main(String[] args) {
int myFirstNumber = 5;
}
}
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, 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 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 what would we need to type two brackets (parentheses) in
between the brackets,
to print out 'myFirstNumber'? 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' .
public class Main { public static void main(String[] args) { int myFirstNumber = 5; System.out.println(myFirstNumber); } }
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 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'.
public class Main { public static void main(String[] args) { int myFirstNumber = 10; System.out.println(myFirstNumber); } }
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.
public class Main { public static void main(String[] args) { int myFirstNumber = 10+5; System.out.println(myFirstNumber); } }
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.
public class Main { public static void main(String[] args) { int myFirstNumber = (10 + 5) + (2 * 10) ; System.out.println(myFirstNumber); } }
And you can see that the answer we're showing is 35.
Photo
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. So what I've done
is I've taken the variable name of myFirstNumber but I've surrounded it in
double-quotes ("").
public class Main { public static void main(String[] args) { int myFirstNumber = (10 + 5) + (2 * 10) ; System.out.println(myFirstNumber); System.out.println("myFirstNumber"); } }
And the hint there is that it changed to 'green'. You know
it's much like the first println with 'Hello World', 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 = 'myFirstNumber + mySecondNumber +
myThirdNumber.
public static void main(String[] args) { int myFirstNumber = (10 + 5) + (2 * 10) ; System.out.println(myFirstNumber); System.out.println("myFirstNumber"); int mySecondNumber = 12; int myThirdNumber = 6; int myTotal = 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 myTotal,
we should get the value of 35 + 12 is 47 + 6 is 53.
So let's 'Run' that.
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.
So how did it go? Did you figure it out? 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. Now just a note
before I finish this lecture, case sensitivity is very important here.
In the next lectures 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 lecture.
If you have any problem, you can comment below.
Happy Coding
Comments
Post a Comment