Mathematical operators in Java

In Java, we have seven mathematical operators including:

  • +: plus
  • : subtraction
  • *: multiplication
  • /: division
  • %: modular division
  • ++: double plus
  • : double subtraction

It is easy for plus, subtraction, multiplication, division, and modular division, isn’t it? So, in this tutorial, I will focus only on two mathematical operators ++ and –.

The operators ++ and – are single operators that increase or decrease the value of a variable by a value of 1. These two operators may be before or after the variable name.

For example:

Or

When these single operators are used in any expression, its position before or after the variable name determines whether the value of the variable associated with it increases or decreases before the expression is calculated. If this single operator is located before the variable name, the value of the variable will increase or decrease by 1 before the expression is calculated, or if it is after the variable name, the value of the variable will increase or decrease after the expression is calculated.

For example, we have an expression that uses the ++ operator as follows:

In the expression int c = b- ++ a, the single ++ operator is located before the variable a, so the value of a in this expression will increase by 1 before the expression is computed. That is, we can rewrite the following:

The result would be -12. And after the expression ends, the value of a will be 21.

Similarly, let’s look at an example with the operator — as follows:

The result after running this expression is that c will be 11 and a will be 8.

Another classic example that we can look at is:

According to you, the results will look like?

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment