# Basic operations with variables in Java

Well, in programming it’s impossible to go without addition, subtraction, multiplication, etc. In Java these arithmetic operations are performed by means of so called “**operators**”. We can divide all Java operators into three groups:

- arithmetic operators
- logical operators
- bitwise operators

In this article we will talk about:

- arithmetic operators
- logical operator (
**in bare outlines**)

Logical and bitwise operators will be covered in our separate articles. At this stage it will be enough for you to understand how do arithmetic operators work. Let’s get started!

**Arithmetical operators**

In fact, **many arithmetic operators are already familiar to you from the school**:

- addition
- subtraction
- multiplication
- division

Let’s try to make sense of these operators using following examples:

**Example 1**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Test { public static void main(String[] args) { int k = 70; int p = 10; int f = 5; int m = k+p; System.out.println(m); m = m-30; System.out.println(m); int s = m*f; System.out.println (s); s =s/f; System.out.println(s); } } |

**If you will try to run this code on your computer, you will see these numbers in the console:**

80

50

250

50

**Comments:**

Let’s look at each code line.

1 2 3 |
int k = 70; int p = 10; int f = 5; |

Here we declared the variable k and assigned the value 70 to it, then we declared the variable p and assigned the value 10 to it, and then we declared another variable f and assigned the value 5 to it.

1 |
int m = k+p; |

There we declared the variable m and assigned a value to it, which is equal to the sum of k and p. K equals 70, p equals 10 and, respectively, m equals 80.

1 |
System.out.println(m); |

Then we displayed the value of m in the console. And, indeed, we see the number 80 in the console.

1 |
m = m-30; |

In this line we specified, that the variable m should get a new value, which equals m - 30. m was equal to 80, so the new value of m will be 80-30, i.e. 50.

1 |
System.out.println(m); |

The new value is displayed in the console and we see, that it equals 50.

1 |
int s = m*f; |

We declare the variable s and assign a value to it, which equals m*f. Since m equals 50 and f is equal to 5, then we multiply 50*5 and get 250. It means, that s equals 250.

1 |
System.out.println (s); |

We assign a new value to the variable s, which equals s/f. In Java the symbol / stands for division. Now the variable s equals 250, f equals 5. Therefore, by dividing 250 by 5 we get 50. Consequently, s equals 250. I.e. the new value of the variable equals 50.

1 |
System.out.println(s); |

We display the new value of the variable s in the console. And, indeed, we see the number 50 in the console.

**Consider this: **In Java the symbol / stands for division

**Shortcut arithmetical operators**

There is a special type of arithmetical operators, which is called “shortcut arithmetical operator”. You need to know that there are two forms of recording of basic arithmetic operations. For example:

m += 7; // is equivalently to m = m+7;

m -= 7 ; // is equivalently to m = m-7;

m*= 7; // is equivalently to m = m*7;

m/= 7; // is equivalently to m = m/7;

**Why were shortcut arithmetical operators implemented? **Because necessity is the mother of invention! When you write many-many code lines, each additional character takes extra time and effort. It’s much easier to use shortcut arithmetical operators…

To make sure that the shortcut arithmetic operators produce the same results as the full form of arithmetic operations, we suggest you to run these two small programs on your computer. **Both Example 2 and Example 3 will display the number 9. **

**Example 2**

1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { int m = 2; m = m + 7; System.out.print (m); } } |

**If you will try to run this code on your computer, the following number will be displayed in your console: **

9

**Example 3**

1 2 3 4 5 6 7 |
class Test { public static void main(String[] args) { int m = 2; m+= 7; System.out.print (m); } } |

**If you will try to run this code on your computer, the following number will be displayed in your console: **

9

Now let’s look at another arithmetic operator – **the remainder operator **or **modulus operator**. We dedicated a separate article to this topic – Remainder operator in Java. And after you will read this article, we will pass on to increment and decrement operators – see below.

**Increment and decrement**

Well, we have already went through operations in Java, which are very similar to those we know from school. Now it’s time to pass on to the operations which will be a novelty for you, unless you have an experience in programming. Although it’s not a rocket science. The case is that you often need to perform such operations:

- to increase the variable by 1
- to decrease the variable by 1

That’s why they invented separate operations with variables, which are called increment and decrement.

**Increment**is responsible for increasing a variable by 1. It is designated as ++. For example, if we have a variable I and we apply the increment to it, then it will be written as i++. That means, that the value of the variable i has to be increased by 1.**Decrement**is responsible for decreasing a variable by 1. It is designated as --. For example, if we have a variable n and we apply the decrement to it, then it will be written as n --. That means, that the value of the variable n has to be decreased by 1.

**Example 4**

1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { int n = 2; n++; System.out.print (n); } } |

**If you will try to run this code on your computer, then you will see the following number in your console:**

3

**Comments:**

In this line we declared the variable n and assigned a value 2 to it.

1 |
int n = 2; |

Then we used the increment and that’s why the variable n has to be increased by 1. And, respectively, from now on the variable n equals 3.

1 |
n++; |

And we displayed the new value of the variable n by means on this line:

1 |
System.out.print (n); |

**Example 5**

1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { int n = 2; n--; System.out.print (n); } } |

**If you will try to run this code on your computer, you will see the following number in our console:**

1

**Comments:**

In this line we declared the variable n and assigned the value 2 to it.

1 |
int n = 2; |

Then we used the decrement and therefore the variable n has to be decreased by 1. And, respectively, from now on the variable n equals 1.

1 |
n--; |

And we displayed the new value of the variable n by means on this line:

1 |
System.out.print (n); |

**Two forms of increment and decrement**

You need to know that there are two forms of increment:

- postfix increment (n++)
*We have already considered this form of increment in Example 4* - prefix increment (++n)

There are two forms of decrement as well:

- postfix increment (n--)
*We have already considered**this form of decrement in Example 5* - prefix increment (--n)

**So what’s the difference between prefix and postfix forms?**

**The postfix form:**

**first uses the old value**in calculations- further in subsequent calculations it uses the new value

**The prefix form:**

**uses the new value in calculations from the very beginning**

It’s better to show it on an example and everything will become clear. Let’s consider the following example:

**Example 6**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Test { public static void main(String[] args) { int n = 2; int k = 2; int a = 2 * n++; //now a equals 4, аnd n equals 3 int b = 2 * ++k; //now b equals 6, аnd k equals 3 System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); } } |

**If you will try to run this code on your computer, then you will see the following numbers in your console:**

4

3

6

3

**Comments:**

1 2 |
int n = 2; int k = 2; |

In these two lines we declared two variables n and k and assigned the same values to them – 2.

1 |
int a = 2 * n++; //now a equals 4, аnd n equals 3 |

In this line we have n++, i.e. the postfix form of increment. That’s why **at first it uses the old value for calculations**. Since n = 2, the variable a = 4. And only after this the increment will be applied to the variable and n will equal 3.

1 |
int b = 2 * ++k; //now b equals 6, аnd k equals 3 |

In this line we have ++k, i.e. the prefix form of increment. That’s why **at first it uses the new value for calculations. **Consequently, k equals 3, i.e. 2*3 = 6 and the variable b equals 6.

1 2 3 4 |
System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); |

By means of these 4 lines we displayed the new values of a, n, b and k:

4

3

6

3

We hope that now the difference between prefix and postfix forms is clear.

**Also you need to remember**, that increment and decrement are applied **only to a variable**, you cannot apply them to numbers. For this reason, expressions like 8++ or ++8 are considered to be unacceptable.

You should consider another example for postfix and prefix forms of decrement. They work on the same principle as postfix and prefix forms of increment. However, here’s the example:

**Example 7**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Test { public static void main(String[] args) { int n = 2; int k = 2; int a = 2 * n--; //now variable a=4, аnd n equals 1 int b = 2 * --k; //now variable b equals 2, аnd k equals 1 System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); } } |

**If you will try to run this code on your computer, then you will see the following numbers in your console:**

4

1

2

1

**Comments:**

1 2 |
int n = 2; int k = 2; |

In these two lines we declared two variables n and k and assigned the same values to them – 2.

1 |
int a = 2 * n--; //now variable a=4, аnd n equals 1 |

In this line we have n--, i.e. the postfix form of decrement. That’s why **at first it uses the old value for calculations**. Since n = 2, the variable a = 4. And only after this the decrement will be applied to the variable and n will equal 1.

1 |
int b = 2 * --k; //now variable b equals 2, аnd k equals 1 |

In this line we have --k, i.e. the prefix form of decrement. That’s why **at first it uses the new value for calculations. **Consequently, k equals 1, i.e. 2*1 = 2 and the variable b equals 2.

1 2 3 4 |
System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); |

By means of these 4 lines we displayed the new values of a, n, b and k:

4

1

2

1

**Logical operators**

The following table lists all the logical operators in Java. We will consider using these operators when we reach our article “Conditional operator if. Operator switch”.

**LET’S SUMMIRIZE:**

All Java operators can be divided into three groups:

- arithmetic operators
- logical operators
- bitwise operators

**Arithmetical and shortcut arithmetical operators**

m += 7; // is equivalently to m = m+7;

m -= 7 ; // is equivalently to m = m-7;

m*= 7; // is equivalently to m = m*7;

m/= 7; // is equivalently to m = m/7;

m%=7;// is equivalently to m = m%7;

- % is the remainder operator. It returns the remainder of division.
- Increment is responsible for increasing a variable by 1. For example, n++.
- Decrement is responsible for decreasing a variable by 1. For example, n--.

There are two forms of increment:

- postfix increment (n++)
*We have already considered this form of increment in Example 4* - prefix increment (++n)
*We have already considered this form of increment in Example 6*

There are two forms of decrement as well:

- postfix increment (n--)
*We have already considered this form of decrement in Example 5* - prefix increment (--n)
*We have already considered this form of decrement in Example 7*

**The postfix form:**

**first uses the old value**in calculations- further in subsequent calculations it uses the new value

**The prefix form:**

**uses the new value in calculations from the very beginning**

increment and decrement are applied only to a variable, you cannot apply them to numbers. For this reason, expressions like 8++ or ++8 are considered to be unacceptable.

We will cover the bitwise operations in a separate article.

*Note: sometimes increment is called incrementing operation, decrement – decrementing operation. *

## Leave a Reply