Operators in Java
In programming, it’s impossible to get by without addition, subtraction, or multiplication. In Java, these arithmetic operations are performed by means of so called “operators.” All Java operators can be divided into three groups:
- arithmetic operators
- logical operators
- bitwise operators
In this article, we will talk about:
- arithmetic operators
- logical operators (in bare outlines)
Logical and bitwise operators will be covered in separate articles. At this stage, all you need to know is how arithmetic operators work. So let’s get started!
Arithmetic operators
Many arithmetic operators are already familiar to you from school:
- addition
- subtraction
- multiplication
- division
Let's try to make sense of these operators in the 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 run this code on your computer, the following numbers will be displayed in your console:
80
50
250
50
Comments:
Let’s look at each line of code.
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 the variable f and assigned the value "5" to it.
1 |
int m = k+p; |
Here we declared the variable m and assigned it a value equal to the sum of k and p. Since k equals 70 and p equals 10, then 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 is m - 30. M had been 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 is m*f. Since m is 50 and f is 5, then we multiply 50*5 and get 250. Thus, s equals 250.
1 |
System.out.println (s); |
We assign a new value to the variable s, which is s/f. In Java, the symbol "/" stands for division. Now the variable s equals 250 and f equals 5. So if we divide 250 by 5, we get 50. Therefore, s equals 250 i.e. the new value of the variable s is 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.
Keep in mind: In Java, the symbol "/" stands for division
Shortcut Arithmetic Operators
There is a special type of arithmetic operators, which is called a “shortcut arithmetic operator.” You need to know that there are two forms to record basic basic arithmetic operations. For example:
m += 7; // means that m = m+7;
m -= 7 ; // means that m = m-7;
m*= 7; // means that m = m*7;
m/= 7; // means that m = m/7;
Why were shortcut arithmetic operators created? Because necessity is the mother of invention! When you write lots of lines of code, each unnecessary character takes up time and effort. It’s simply much easier to use shortcut arithmetic operators…
To make sure that the shortcut arithmetic operators are producing the same results as full-form arithmetic operations, we suggest that you 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 run this code on your computer, you will see the following number 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 run this code on your computer, you will see the following number displayed in your console:
9
Now let’s look at another arithmetic operator – the remainder operator or modulus operator. We've dedicated a separate article to this topic – the remainder operator in Java. After you read this article, we'll move on to increment and decrement operators.
Increment and Decrement Operators
Well, we have already examined operations which resemble the ones we learned in school. Now it’s time to head onto operations which you may found strange, unless you programmed before. But don't worry; it’s not rocket science.
In programming, 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 for variables, which are called increment operators and decrement operators.
- Increment operators increase a variable by 1. They are designated as ++. For example, if we have variable "i" and we apply the increment to it, then it will be written as i++. This means that the value of the i variable will increase by 1.
- Decrement operators decrease a variable by 1. They are designated as --. For example, if we have variable "n" and we apply the decrement to it, then it will be written as n --. This means that the value of the n variable will decrease 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 run this code on your computer, you will see the following number displayed in your console.
3
Comments:
In this line, we declared the n variable and assigned the value "2" to it.
1 |
int n = 2; |
Then we used the increment operator and that’s why the n variable increased by 1. And, consequently, the n variable will equal 3 from now on.
1 |
n++; |
And with the help of this line, we display the new value of the n variable:
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 run this code on your computer, you will see the following number displayed in your console.
1
Comments:
In this line we declared the n variable and assigned the value "2" to it.
1 |
int n = 2; |
Then we used the decrement operator and, therefore, the n variable will decrease by 1. From now on, the n variable will equal 1.
1 |
n--; |
And with the help of this line, we display the new value of the n variable.
1 |
System.out.print (n); |
Two forms of Increment and Decrement Operators
You need to know that there are two forms of increment operators:
- post-increment operators (n++) We just covered this increment operator form in Example 4.
- pre-increment operators (++n)
There are two forms of decrement as well:
- post-decrement operators (n--) We just covered this decrement operator form in Example 5,
- pre-decrement operators (--n)
So what’s the difference between the pre- and post- forms?
The post- form:
- first uses the old value in calculations
- uses the new value in subsequent calculations
The pre- form:
- uses the new value in calculations from the very beginning
It’s best to show this in an example and everything will be 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 run this code on your computer, you will see the following numbers displayed in your console:
4
3
6
3
Comments:
1 2 |
int n = 2; int k = 2; |
In these two lines, we declared the two variables "n" and "k" and assigned them the same value: 2.
1 |
int a = 2 * n++; //now a equals 4, аnd n equals 3 |
In this line, we have n++ i.e. the post-increment operator. That’s why it used the old value for the first calculation. Since n = 2, variable a = 4. After this initial calculation, the increment operator 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 pre-increment operator. That’s why, from the very beginning, it uses the new value for calculations from. Consequently, k equals 3, i.e. 2*3 = 6 and variable b is 6.
1 2 3 4 |
System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); |
With the help of these 4 lines, we display the new values of a, n, b and k:
4
3
6
3
We hope that you now understand the difference between post- and pre- forms.
Also, you need to remember that increment and decrement operators are applied only to a variable. You cannot apply them to numbers. Therefore, you can write 8++ or ++8.
Let's consider another example of post- and pre- forms of decrement operators. They work according to the same principle as post- and pre- forms of increment operators.
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 run this code on your computer, you will see the following numbers displayed in your console:
4
1
2
1
Comments:
1 2 |
int n = 2; int k = 2; |
In these two lines, we declared the variables "n" and "k" and assigned them the same value: "2."
1 |
int a = 2 * n--; //now variable a=4, аnd n equals 1 |
In this line, we have n-- i.e. the post-decrement operator. That’s why it initially uses the old value for calculations. Since n = 2, variable a = 4. After this initial calculation, 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 pre-decrement operator. That’s why, from the very beginning, it uses the new value for calculations. Consequently, k equals 1, i.e. 2*1 = 2 and variable b equals 2.
1 2 3 4 |
System.out.println(a); System.out.println(n); System.out.println(b); System.out.println(k); |
With the help of these 4 lines, we display the new values of a, n, b and k. And then we get:
4
1
2
1
Logical Operators
The following table lists all the logical operators in Java. We will cover these operators when we reach the article “Conditional operator if. Operator switch”.
LET’S SUMMIRIZE:
All Java operators can be divided into three groups:
- arithmetic operators
- logical operators
- bitwise operators
Arithmetic and Shortcut Arithmetic Operators
m += 7; // means that m = m+7;
m -= 7 ; // means that m = m-7;
m*= 7; // means that m = m*7;
m/= 7; // means that m = m/7;
m%=7;// means that m = m%7;
- % is the remainder operator. It returns the remainder of division.
- Increment Operators increase a variable by 1. For example, n++.
- Decrement Operators decrease a variable by 1. For example, n--.
There are two forms of increment operators:
- post-increment operators (n++) We covered this increment operator form in Example 4.
- pre-increment operators (++n) We covered this increment operator form in Example 6.
There are two forms of decrement operators as well:
- post-increment operators (n--) We covered this decrement operator form in Example 5.
- pre-increment operators (--n) We covered this decrement operator form in Example 7.
The post- form:
- initially uses the old value in calculations
- uses the new value in subsequent calculations
The pre- form:
- uses the new value in calculations from the very beginning
Increment and decrement operators can only be applied to variables; you cannot apply them to numbers. For this reason, you can't write expressions like 8++ or ++8.
We will cover bitwise operations in a separate article.