You can update the value inside a variable.
You saw that defining a variable and storing a value is pretty simple.
You need to write the type of value we can store, this variable in particular can only store integer
values.
You also need to write the variable name and the value that you want to store.
So available stores information about your app, but as time passes, things change and you need to
update that information.
That's why in this video, you're going to learn to update the value inside a variable.
First thing I need you to do is create a new class by yourself inside the section to project make a
new file named Buster Java and then make sure the bus class has the main method.
Take a couple of seconds to do that.
All right, how do you update variables if you want to update a variable, it's fairly simple.
Each have to set it equal to a new value and the value inside changes every time you update it.
I prepared a fun example so that we can learn more about this, you, the Java prodigy, you found a
job at the Ministry of Transportation in the ministry wants you to build a Java program, the programming
you to print, how many passengers are inside a bus, and then you need to print the number of passengers
at every stop.
So I'm going to assume you ready set the class up and before we start our code, you'll notice that
the terminal output is pretty cluttered.
You could just close the terminal, open a new terminal, but that's pretty lame if you ask me.
We can instead clear the terminal output by writing.
Clear.
Nice, this command is very useful, and I'm going to add it to your cheat sheet.
OK, so the bus driver is starting his shift.
He boarded the bus and naturally the bus is empty and he starts with zero passengers and passengers
is equal to zero.
Don't forget your semicolon.
All right, his first stop is the river station, let's say there he picks up nine passengers.
So we need to update the number of passengers by nine passengers.
Is equal to passengers.
Plus nine.
Then after you update the number of passengers, you can print the new value system, dot out dot print
line passengers.
All right, compiling the code, Java Sea Bass Java and running it.
Clearly now there are nine passengers in the second line, you said passengers equal to itself, zero
plus nine, and now it stores a value of nine.
Now the bus driver is approaching the Sugar Mountain sweetshop in, your passengers go crazy for sweets.
So there he drops off five passengers.
Once again, you need to update the number of passengers that is passengers is equal to passengers minus
five.
And after you update the value print system, dot, dot, dot, print line passengers.
And using the up key, go ahead and compile the code.
All right, we started with zero passengers.
The bus driver picked up nine and dropped off five.
So the pattern follows four subtraction, the computer sets the value of passengers equal to itself,
nine minus five.
Now, there are four passengers in the bus and let's say the last stop is the post office and they're
the bus driver drops off the remaining passengers.
So passengers equal to passengers minus four.
And we're going to print the value system, dot, dot, dot, print line passengers.
Recompile your code and run it.
And everything works out as expected.
Your application updates information at every stop.
The bus starts with zero passengers, picks up nine.
Drops out five.
And drops the rest.
This is a pretty common way of updating integer variables, but there is an easier way.
And it's known as the plus equals operator, the plus equals operator updates a variable by adding the
value on the right.
So you can replace the first update with the plus equals operator passengers plus equals nine.
And if you ask me, this looks a lot better.
Let's see if it works.
And indeed, it does.
Passenger starts at zero and the plus equals operator updates the variable by adding nine fairly intuitive.
Now, what about these updates?
Well, there's also the minus equals operator.
This one, as you'll probably guess, it's tracks the value on the rights.
So you can replace the remaining updates with the minus equals operator.
Recompile the code.
And everything works as expected.
In both cases, it appears the variable by subtracting the value on the right.
And in this lesson, you learn to update the value inside a variable, you built a Java program that
updates the number of passengers when the bus picks up or drops passengers off plus equals and minus
equal to update the variable using the value on the right.
And that is all.