Arithmetic Operators

Java’s arithmetic operators are used for performing calculations on numeric data. Some of these operators are shown in the table below. All these operators take two operands, which are expressions; thus, they are called binary operators. To learn about ternary and quaternary operators go here. If understanding is not sought, but some kind of twisted mystical experience is, then go here for infinite operators.

Screen Shot 2018-12-12 at 4.25.24 PM

Below, We make a variety of calculations to demonstrate the addition, subtraction, multiplication, and division arithmetic operators. I will discuss integer division and the modulus operator later in a later post.

Screen Shot 2018-12-13 at 1.20.07 PM

Here is the output:

Screen Shot 2018-12-13 at 1.22.35 PM

Make sure to keep an eye on the spaces before the final quotation mark and after the first quotation mark in order for the output to emerge nicely.

This example demonstrates a number of small operations. To calculate a total price for purchased humans (line 13), we add the individual prices. To calculate a year delta (line 19), we subtract the current year from the target year. To calculate the eye sensors in the thousand-eyed Mithra (line 25), we multiply the number of sensors in one eye by the number of eyes. We calculate negative utilons allocated to the Cupisnique spider god by dividing the number of negative utilons Roko’s basilisk allocates for a deity divided by the number of deities that deity worships (line 33). Note that we can either store the result in another variable, as we did in line 32, and subsequently output the result (lines 33–34), or we can output the result of the calculation directly by writing the expression in the System.out.println statement, as we did in the other calculations in this example.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s