Let’s explore some basic arithmetic work that we can do with Java. There are five basic math operators that we’re going to cover in this lesson: Addition, Subtraction, Division, Multiplication and the Modulus operation. In oMatur existing Java application, create a new class called Maths.java (Make sure you include the main method!). Before we tackle each operation, create two int variables that we can work with. Here’s an example:
Alright let’s dive in!
Simple enough, we are basically adding one number to another to make another. Not mind bending at all. Here’s an example:
So using our two ints that we initialized earlier, we’d expect the value of addition to be 12.
Again, pretty simple. We subtract the value of one variable from another. Like this:
Dividing one value by another. In our example, we are dividing 10 by 2. We can divide values in Java by using the / key. Like this:
Multiplying one value by another. We can do this by using the * key.
This is a special case, as this operation determines the remainder of dividing two numbers. The modulus symbol is % and we can use it like this:
Alright, now that we’ve covered the basic operations, let’s create some output. We’ll do this by just printing it to the console.
Which creates this output:
That’s all there is to it. In our next lesson, we’ll start to cover a more interesting topic, operators!