We can use variables to store values in the getting started section, you learn to create and run a
job, a program you ran, a fairly simple program that prints Hello, Java.
Now, one more useful program is able to store information, and that's why in this lesson, you're
going to learn to store values inside variables.
First thing I'll need you to do is create a new Java project by yourself and call it Section two, then
launch it from voice code.
And inside the project, critical name variables Jebba and make sure the variables class has the main
method.
All right, what is a variable?
A variable can store value.
Think of a variable as some kind of box, each variable has a name, so I'll call this one food.
This variable, this quote unquote, box can store values.
This one store is a tax value.
But it can also store an integer value, by the way, integer means hole number, the take home message
is that a variable can store any type of value instead of make a variable in Java, you need to include
three pieces of information, the type of value we can store, the name of the variable and the value
itself.
And to this code creates a variable called people, the variables of type int so we can only store numbers
like the number two.
Some inside main making into variable named people and set it equal to 20 different people.
Is equal to 20 and don't forget your semicolon.
Remember that every line of code in Java needs a semicolon at the end.
Otherwise, your code is not going to run.
And so now this variable stores the number 20.
Now, I want to talk about the convention to naming variables and there's a specific way to name them,
and it's called Lower Kemel case.
In Java, the variable needs to start with a lowercase letter.
The variable people starts with a lower case P, but what if we have more than one word like people
on bus?
In that case, you'll start with a lower case letter, but the words that follow must be upper case.
Notice that people start to they lower case P.
And on bus, half capital letters.
This right here is the lower camel case format, and that's how you're supposed to name variables,
if in my code I decide to rename this variable to people on Busse, I would have to use the lower Camil
case format, people on bus, yet people on buses.
Dulong I prefer saying passengers, so I'll rename the variable to passengers.
Remember to write class names in Camil case, where each word is capitalized and joined together and
always write variable names and lower camel case were the first word is lowercase, but the words that
follow are capitalized.
I highly encourage you to follow these conventions because it's what the professionals do and it makes
your code clearer and easier to read.
OK, so we talked about variables being able to store values, but you can actually print the value
inside a variable.
You created a variable called passengers and it stores the value 20.
So using print line, you can print the variables value to the console system.
Print line passengers.
Now, you can go ahead and open up the terminal, compile your code using job C variables Java.
And run the compiled code using Java variables.
Sure enough, the value inside passengers is 20.
Now, here's a similar code where I didn't name passed in your stores the value five instead of 20,
then you printed the value inside passengers.
Java is strongly typed once you define the valley type or verbal can only store values of that type.
For example, you cannot stop text in an integer variable that's illegal, the value that you store
needs to be consistent with the variable type.
You try to store value that is different from the variable type, you will get a type mismatch.
In other words, your codes not going to compile.
So back to my code, I'm going to try to set passengers equal to some.
Text value.
Let's try to Kampala's.
And I get an error in line three of my code and it says that string cannot be converted to it.
This error is a result of trying to store text in an integer variable.
And invariable can only store integer values like five, you cannot store text.
So compiling this code and running it were good, by the way, in case you forgot, the reason I'm able
to run these terminal commands so quick is because I'm using the up key to access previous terminal
commands.
You would have already seen this in the workbook one point one, as well as your very first quests.
In any case, Java is case sensitive, which means Java discriminates between uppercase and lowercase
letters, for example, passengers is not the same as capital passengers.
I try to compile this, I would get an error in line four of my code and it tells me that Java does
not recognize the variable capital passengers because you never made one.
You must always stay consistent with letter cases.
So we're going to fix this variable back to what it was.
All right.
Finally, you can set variables equal to each other if you set a variable equal to another, the variable
copies of the value inside the first one, just like you're seeing here.
So make an integer variable name bus tickets, remember that you need to write variables in lower camil
case lower case B in the words that follow must be upper case bus tickets.
Now, let's assume to enter the bus, every passenger needs one bus ticket.
So there are as many bus tickets as there are passengers and bus tickets is equal to passengers.
Now we're going to check the value inside bus tickets by printing it system dot out, dot print line
bus tickets, I would expect this to print five, but let's double check Gervasi variables that Gever.
Job of variables and as expected, it prints five bus tickets and five passengers.
Great.
Here's the code we have so far.
By setting the bus tickets variable equal to passengers, it copies the value inside.
In this lesson, you learn to store values inside variables, you stored the integer value five inside
passengers and then you print it out using the print online function.
You also learn that variables can only store values of their type.
For example, you cannot store text in an integer variable.
The last thing you learned was the set variables equal to each other.
You set the bus ticket variable equal to passengers and it copied the value inside.