In this article you will learn about Java operators. You will look at what is an operator, types of operators in Java and an example for each of the Java operators.
An operator allows the programmer or the computer to perform an operation on the operands. An operand can be a literal, variable or an expression.
Operators can be divided along two dimensions: 1) number of operands on which the operator works and 2) type of operation the operator performs.
Based on number of operands, operators in Java can be divided into three types:
- Unary operator (works on single operand)
- Binary operator (works on two operands)
- Ternary operator (works on three operands)
Based on the type of operation performed, operators are divided into five categories as shown below:
Arithmetic operators are frequently used operators in the Java programs. They are used to perform basic mathematical operations like addition, subtraction, multiplication and division.
All the arithmetic operators and an example for each of them is provided below:
I think that the above table is self explanatory. The modulus (%) operator gives the remainder value of the division.
Let’s focus on the increment (++) and decrement (–) operators. They are both unary operators, means, they operate on a single operand. Based on whether the increment or decrement operator is placed before or after the operand, they are divided into two types: 1) Pre increment or Pre decrement and 2) Post increment or Post decrement. They will look like as shown below:
++a (Pre increment)
– – a (Pre decrement)
a++ (Post increment)
a – – (Post decrement)
Pre and post increment or decrement have different behaviors when used in assignment expressions. Their behavior is explained in the below example:
a = 10;
b = a++; //a value will be assigned to b and then a will be incremented by 1. So, b value is 10
a = 10;
b = ++a; //a value is incremented by 1 and then assigned to b. So, b value is 11
The above example only shown post and pre increment. Similar is the behavior for post and pre decrement.
The operators +=, -=, *=, /= and %= are known as shorthand assignment operators. All they do is save you from typing two more extra characters 😉
Consider the following example which shows you how to use the shorthand assignment operator:
int a = 10;
a += 2; //Same as writing a = a + 2
a += 1; // Same as writing a++
Similar is the behavior of other compound assignment operators. Just experiment with them 🙂
Arithmetic operators work only on numeric types and characters. The char is originally a sub type of int.
Bitwise operators as the name implies works on bits of the value. These operators can be applied on integer types and character type.
Every integer value is represented as a combination of 0’s and 1’s (binary) inside the memory. For example the integer value 18 of type byte is represented in memory as shown below:
0 0 0 1 0 0 1 0
If you want to modify the value at bit-level then you can use the bit-wise operators. All the bit-wise operators and an example for each of them is provided below:
Bitwise logical operators ~, &, | and ^ operate on two bits as per the table shown below:
Read more about bitwise operators here
Relational operators are used to compare two values. The result of comparing two values is always a boolean value true or false. The relational operators available in Java and an example for each operator is shown below:
Relational operators are generally used in control statements which will be explained in another article. Unlike C and C++, true doesn’t refer any positive value other than zero and false doesn’t refer zero. So, writing while(1) to repeat a loop continuously doesn’t work in Java even though it works in C and C++.
Logical operators are used to evaluate two boolean expressions or values and return the resultant boolean (truth) value. All the logical operators and an example for each logical operator is given below:
The logical AND, OR, XOR and NOT work in similar fashion as the bitwise AND, OR, XOR and NOT except that the operands for logical operators are boolean values. The truth table for the logical operators is given below:
The short-circuit AND (&&) and short-circuit OR (||) are special operators in Java. Special in the sense, in the case of short-circuit AND (&&), if the left operand evaluates to false, the right operand is not evaluated and is ignored. Similarly for short-circuit OR (||), if the left operand evaluates to true, the right operand is not evaluated and is ignored.
Below is an example which demonstrates the use of short-circuit AND (&&) to eliminate a runtime exception caused due to dividing a number by zero:
if(denom != 0 && num/denom > 10)
In the above piece of code, if denom !=0 gives false, then the right side expression of && is not evaluated there by eliminating the chance of exception.
The assignment operator is represented by =. It is used for assigning a value to a variable as shown below:
int a = 10;
Java provides a special ternary operator that can be used as an replacement for if-then-else selection statement. The conditional operator is represented as ?:
The syntax for using the conditional operator is shown below:
expression 1 ? expression 2 : expression 3
Always expression 1 must evaluate to a boolean value. If the result of expression 1 is true, then expression 2 is evaluated or else if expression 1 is false, expression 3 is evaluated and the resulting value will be returned. Consider the following example which demonstrates the use of conditional or ternary operator:
int a = 1, b = 1;
int result = (a==1 && b==1) ? 1 : 0;
The value stored in result variable will be 1 as the expression a==1&&b==1 evaluates to true.