Advertisement
English transcript
This line of code's responsible for creating a variable.
Remember that a variable is like this box, number of coffees,
with a value of two inside.
We'll talk about each part of this line of code and
then we're going to create more variables in this lesson.
You may have noticed there's specific rules for how to declare a variable.
When I say declare a variable it means the same thing as defining a variable or
creating a variable.
This is the line of code that we've seen so far.
It follows a very specific format for how to declare variables in Java.
You could think of it almost like a formula.
First, we need to say what type of data can go inside the variable.
In this case, it's an integer, which we call int for short.
Next is the variable name.
In our case,
the variable name is numberOfCoffees followed by the initial value.
In this case it's 2; for 2 cups of coffee ordered, and
this code statement ends with a ; according to the rules of Java.
Let's break down this line one piece at a time.
The first word is Data type,
this is a type of data that's allowed to go inside at this variable.
In this case we only let integer numbers.
Gets stored inside the variable.
As I mentioned earlier int is short for integer.
An integer is a whole number.
Basically any number including zero and
negative numbers that are not fractions or decimals.
0, 5, 145, all of these are examples of integers.
The number of coffees ordered should only be a whole number like one,
two, four, et cetera.
We don't want the customer to order 1.5 cups of coffee.
If they want more than one cup, they're going to have to buy two,
then they can give the remaining half to a friend or something.
As for negative numbers, it doesn't make as much sense in our case.
There's no data type in Java that only allows positive numbers.
So later in the next lesson, we're going to learn how to add code
that prevents negative numbers from going inside the variable.
The next part of the declaration is the Variable name.
In this case it's numberOfCoffees.
Notice that it's spelled camel case.
It starts with a lowercase letter and since there are multiple words,
it capitalizes the start of each other word.
You can think of it as putting a name tag on the variable.
You can refer to this variable with this name.
And here I've drawn it out like this as well.
This variable box has this name.
If you want to know what this value is.
Or change it, you need to use the name.
Choosing an appropriate variable name is actually up to you the developer,
depending on what the variable is used for.
In this case, since the variable is storing the numberOfCoffees ordered
I just picked this variable name.
You could have picked other names, like coffee count or
number of coffees ordered or ordered coffees.
Really it could be anything, as long as it's easy for
other people to understand what this variable represents.
When I say that the variable name can be anything,
there's actually a couple of rules that Java has about variable names.
Let's look up those rules now.
Let me type in variable names Java.
This first link looks good.
This is official Java documentation from Oracle.
This top part talks about variables,
which you can read in your infinite free time.
Down here is what we're interested in.
It talks about naming for variables.
It can be any length, but a really long name is not very practical.
A really short name, on the other hand, can be good, but
if it's only one character, like N, you may not know what n stands for.
So you want to strike a balance between a descriptive name and
one that isn't a pain to type out every single time.
There are a bunch more details here that you can read if you want.
But the main point is that if your name only consists of one word
then spell out that word in all lower case letters.
Like cadence, speed, gear, things like that.
If the variable name consists of more than one word then you want to
capitalize the first letter of each subsequent word but
it needs to start with a lower case first.
So gear ratio is an example or current gear.
Our example of number of coffees also follows this rule.
The next part of the declaration is Assignment operator or the = sign.
In math class, you're used to seeing things like one plus one equals two.
But in Java, equals means a different thing.
Equal means assigning a value to a variable.
In this case, we're assigning the value 2 to the variable numberOfCoffees.
Using our box metaphor,
we put the number 2 inside the variable that represents number of coffees.
So whenever you see the equals sign,
think about grabbing what's on the right-hand side of it and
putting it inside the variable on the left-hand side.
We already talked a little bit about this, but
this two here is the initial value of the variable.
So you put the 2 inside this variable.
Remember that this initial value must be of a valid type
according to this data type.
In this case only integers are allowed in this variable.
2 is a valid integer so it's okay for it to go inside this variable.
And last but not least, we have this ; at the very end.
So to summarize, when you declare a variable,
you need to specify the data type, followed by the variable name,
and then an equal sign, and then an initial value, and then a semicolon.
In this case, we're creating a variable called numberOfCoffees.
The data type is int, so only integers can be stored in this variable.
And we're assigning the value 2 to be the initial value of this variable.
Or you can change the variable name according to the rules we talked about.
And you can change the initial value as long as it's the same data type.
You can't change this data type, it needs to be spelled int, I-N-T.
We're going to be learning about other data types as well,
and those have to be spelled a specific way.
Now any time you need to use a variable, just refer to it by it's name, and
then it will be swapped out for
the actual value later when the app is actually running.
Now I want you to go ahead and
add these lines of code to your app, please don't copy and past though,
because I want you to practice typing out, creating and using a variable.
Then go ahead and run the app on your phone.
Make sure that when you hit the Order button, this is what you see.
After that, I want you to do a little bit of experimenting.
Try to assign a new initial value to the variable.
Then go ahead and try to assign a new variable name.
Instead of numberOfCoffees,
try something else like quantity for example.
A common beginner mistake is to think that you typed it out correctly
because your eyes are scanning over the whole thing really quickly.
But if you look individually at every single character you
would notice that there might be a problem.
This will take you some time to finish but take your time and
pay close attentions to each character that you type.
If you run into problems, try reading the error message.
For example, this error message says cannot resolve symbol numberOCoffees.
Java doesn't recognize what numberOCoffees is
because the variable name was actually numberOfCoffees.
Hopefully you can figure out from the error or you can hit undo and
that sometimes resolves the error as well.
Or you can go back to code that you know works, like the code that we gave you.
When you're done, check these boxes and continue.
Remember that a variable is like this box, number of coffees,
with a value of two inside.
We'll talk about each part of this line of code and
then we're going to create more variables in this lesson.
You may have noticed there's specific rules for how to declare a variable.
When I say declare a variable it means the same thing as defining a variable or
creating a variable.
This is the line of code that we've seen so far.
It follows a very specific format for how to declare variables in Java.
You could think of it almost like a formula.
First, we need to say what type of data can go inside the variable.
In this case, it's an integer, which we call int for short.
Next is the variable name.
In our case,
the variable name is numberOfCoffees followed by the initial value.
In this case it's 2; for 2 cups of coffee ordered, and
this code statement ends with a ; according to the rules of Java.
Let's break down this line one piece at a time.
The first word is Data type,
this is a type of data that's allowed to go inside at this variable.
In this case we only let integer numbers.
Gets stored inside the variable.
As I mentioned earlier int is short for integer.
An integer is a whole number.
Basically any number including zero and
negative numbers that are not fractions or decimals.
0, 5, 145, all of these are examples of integers.
The number of coffees ordered should only be a whole number like one,
two, four, et cetera.
We don't want the customer to order 1.5 cups of coffee.
If they want more than one cup, they're going to have to buy two,
then they can give the remaining half to a friend or something.
As for negative numbers, it doesn't make as much sense in our case.
There's no data type in Java that only allows positive numbers.
So later in the next lesson, we're going to learn how to add code
that prevents negative numbers from going inside the variable.
The next part of the declaration is the Variable name.
In this case it's numberOfCoffees.
Notice that it's spelled camel case.
It starts with a lowercase letter and since there are multiple words,
it capitalizes the start of each other word.
You can think of it as putting a name tag on the variable.
You can refer to this variable with this name.
And here I've drawn it out like this as well.
This variable box has this name.
If you want to know what this value is.
Or change it, you need to use the name.
Choosing an appropriate variable name is actually up to you the developer,
depending on what the variable is used for.
In this case, since the variable is storing the numberOfCoffees ordered
I just picked this variable name.
You could have picked other names, like coffee count or
number of coffees ordered or ordered coffees.
Really it could be anything, as long as it's easy for
other people to understand what this variable represents.
When I say that the variable name can be anything,
there's actually a couple of rules that Java has about variable names.
Let's look up those rules now.
Let me type in variable names Java.
This first link looks good.
This is official Java documentation from Oracle.
This top part talks about variables,
which you can read in your infinite free time.
Down here is what we're interested in.
It talks about naming for variables.
It can be any length, but a really long name is not very practical.
A really short name, on the other hand, can be good, but
if it's only one character, like N, you may not know what n stands for.
So you want to strike a balance between a descriptive name and
one that isn't a pain to type out every single time.
There are a bunch more details here that you can read if you want.
But the main point is that if your name only consists of one word
then spell out that word in all lower case letters.
Like cadence, speed, gear, things like that.
If the variable name consists of more than one word then you want to
capitalize the first letter of each subsequent word but
it needs to start with a lower case first.
So gear ratio is an example or current gear.
Our example of number of coffees also follows this rule.
The next part of the declaration is Assignment operator or the = sign.
In math class, you're used to seeing things like one plus one equals two.
But in Java, equals means a different thing.
Equal means assigning a value to a variable.
In this case, we're assigning the value 2 to the variable numberOfCoffees.
Using our box metaphor,
we put the number 2 inside the variable that represents number of coffees.
So whenever you see the equals sign,
think about grabbing what's on the right-hand side of it and
putting it inside the variable on the left-hand side.
We already talked a little bit about this, but
this two here is the initial value of the variable.
So you put the 2 inside this variable.
Remember that this initial value must be of a valid type
according to this data type.
In this case only integers are allowed in this variable.
2 is a valid integer so it's okay for it to go inside this variable.
And last but not least, we have this ; at the very end.
So to summarize, when you declare a variable,
you need to specify the data type, followed by the variable name,
and then an equal sign, and then an initial value, and then a semicolon.
In this case, we're creating a variable called numberOfCoffees.
The data type is int, so only integers can be stored in this variable.
And we're assigning the value 2 to be the initial value of this variable.
Or you can change the variable name according to the rules we talked about.
And you can change the initial value as long as it's the same data type.
You can't change this data type, it needs to be spelled int, I-N-T.
We're going to be learning about other data types as well,
and those have to be spelled a specific way.
Now any time you need to use a variable, just refer to it by it's name, and
then it will be swapped out for
the actual value later when the app is actually running.
Now I want you to go ahead and
add these lines of code to your app, please don't copy and past though,
because I want you to practice typing out, creating and using a variable.
Then go ahead and run the app on your phone.
Make sure that when you hit the Order button, this is what you see.
After that, I want you to do a little bit of experimenting.
Try to assign a new initial value to the variable.
Then go ahead and try to assign a new variable name.
Instead of numberOfCoffees,
try something else like quantity for example.
A common beginner mistake is to think that you typed it out correctly
because your eyes are scanning over the whole thing really quickly.
But if you look individually at every single character you
would notice that there might be a problem.
This will take you some time to finish but take your time and
pay close attentions to each character that you type.
If you run into problems, try reading the error message.
For example, this error message says cannot resolve symbol numberOCoffees.
Java doesn't recognize what numberOCoffees is
because the variable name was actually numberOfCoffees.
Hopefully you can figure out from the error or you can hit undo and
that sometimes resolves the error as well.
Or you can go back to code that you know works, like the code that we gave you.
When you're done, check these boxes and continue.