One of the most fundamental uses of a computer is to perform mathematical operations. As a computer language, Java provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Other Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators.
Assume integer variable A holds 10 and variable B holds 20, then:
| Operator | Description | Example |
|---|---|---|
| + | Addition - Adds values on either side of the operator | A + B will give 30 |
| - | Subtraction - Subtracts right operand from the left operand | A - B will give -10 |
| * | Multiplication - Multiplies values on either side of the operator | A * B will give 200 |
| / | Division - Divides left operand by the right operand | B / A will give 2 |
| % | Modulus - Divides left operand by the right operand and returns remainder | B % A will give 0 |
| ++ | Increment - Increases the value of operand by 1 | B++ or ++B will give 21 (see below) |
| -- | Decrement - Decreases the value of operand by 1 | B-- or --B will give 19 (see below) |
Example
The following simple example program demonstrates the arithmetic operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Example
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("b / a = " + (b / a));
System.out.println("b % a = " + (b % a));
System.out.println("c % a = " + (c % a));
System.out.println("a++ = " + (a++));
System.out.println("a-- = " + (a--));
System.out.println("d++ = " + (d++));
System.out.println("++d = " + (++d));
}
}
This will produce the following result:
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++ = 10
a-- = 11
d++ = 25
++d = 27
Increment and Decrement Operators
1. The increment (++) and decrement (--) operators are special arithmetic operators. While arithmetic operators require two operands for operation, the increment and decrement operators require only one operand.
Example
public class selfAddMinus {
public static void main(String[] args) {
int a = 3;
int b = ++a;
int c = 3;
int d = --c;
System.out.println("Value after increment: " + b);
System.out.println("Value after decrement: " + d);
}
}
This will produce the following result:
Value after increment: 4
Value after decrement: 2
Analysis:
- int b = ++a; The operation process is split as: a = a + 1 = 4; b = a = 4, so the final result is b = 4, a = 4.
- int d = --c; The operation process is split as: c = c - 1 = 2; d = c = 2, so the final result is d = 2, c = 2.
2. Prefix Increment/Decrement (++a, --a): The increment or decrement operation is performed first, then the expression is evaluated.
3. Postfix Increment/Decrement (a++, a--): The expression is evaluated first, then the increment or decrement operation is performed.
Example
public class selfAddMinus {
public static void main(String[] args) {
int a = 5;
int b = 5;
int x = 2 * ++a;
int y = 2 * b++;
System.out.println("After prefix increment, a = " + a + ", x = " + x);
System.out.println("After postfix increment, b = " + b + ", y = " + y);
}
}
This will produce the following result:
After prefix increment, a = 6, x = 12
After postfix increment, b = 6, y = 10
Relational Operators
The following table lists the relational operators supported by Java.
Assume integer variable A holds 10 and variable B holds 20, then:
| Operator | Description | Example |
|---|---|---|
| == | Checks if the values of two operands are equal or not, if yes then condition is true. | (A == B) is false. |
| != | Checks if the values of two operands are equal or not, if values are not equal then condition is true. | (A != B) is true. |
| > | Checks if the value of left operand is greater than the value of right operand, if yes then condition is true. | (A > B) is false. |
| < | Checks if the value of left operand is less than the value of right operand, if yes then condition is true. | (A < B) is true. |
| >= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition is true. | (A >= B) is false. |
| <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition is true. | (A <= B) is true. |
Example
The following simple example program demonstrates the relational operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Test.java File Code:
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b));
System.out.println("a != b = " + (a != b));
System.out.println("a > b = " + (a > b));
System.out.println("a < b = " + (a = a = " + (b >= a));
System.out.println("b <= a = " + (b <= a));
}
}
This will produce the following result:
a == b = false
a != b = true
a > b = false
a = a = true
b <= a = false
Bitwise Operators
Java defines bitwise operators, which can be applied to the integer types, long, short, char, and byte.
Bitwise operators work on bits and perform bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A & B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators. Assume integer variable A holds 60 and variable B holds 13 then:
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND operator. It copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |
| | | Bitwise OR operator. It copies a bit if it exists in either operand. | (A | B) will give 61 which is 0011 1101 |
| ^ | Bitwise XOR operator. It copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |
| ~ | Bitwise compliment operator. It flips the bits. | (~A) will give -61 which is 1100 0011 in 2's complement form due to a signed integer. |
| << | Left shift operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
| >> | Right shift operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 1111 |
| >>> | Unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. | A >>> 2 will give 15 which is 0000 1111 |
Example
The following simple example program demonstrates the bitwise operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Test.java File Code:
public class Test {
public static void main(String[] args) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
System.out.println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */
System.out.println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
System.out.println("a <> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );
c = a >>> 2; /* 15 = 0000 1111 */
System.out.println("a >>> 2 = " + c );
}
}
This will produce the following result:
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a <> 2 = 15
a >>> 2 = 15
Logical Operators
The following table lists the logical operators. Assume Boolean variables A holds true and variable B holds false then:
| Operator | Description | Example |
|---|---|---|
| && | Called Logical AND operator. If both the operands are non-zero, then the condition is true. | (A && B) is false. |
| || | Called Logical OR Operator. If any of the two operands is non-zero, then the condition is true. | (A || B) is true. |
| ! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make it false. | !(A && B) is true. |
Example
The following simple example program demonstrates the logical operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Example
public class Test {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b));
System.out.println("!(a && b) = " + !(a&&b));
}
}
This will produce the following result:
a && b = false
a || b = true
!(a && b) = true
Short-circuit Logical Operators
When using the logical AND operator, the result is true only if both operands are true. However, if the first operand evaluates to false, the result is guaranteed to be false, so the second operand is not evaluated.
Example
public class LuoJi {
public static void main(String[] args) {
int a = 5;
boolean b = (a<4) && (a++<10);
System.out.println("Using short-circuit logical operator result is " + b);
System.out.println("Value of a is " + a);
}
}
This will produce the following result:
Using short-circuit logical operator result is false
Value of a is 5
Analysis: This program uses the short-circuit logical AND operator (&&). First, it evaluates a<4, which is false. Therefore, the result of b is guaranteed to be false, so the second operand a++<10 is not evaluated. Thus, the value of a remains 5.
Assignment Operators
Following are the assignment operators supported by Java language:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C |
| += | Add and assignment operator. It adds right operand to the left operand and assign the result to left operand. | C += A is equivalent to C = C + A |
| -= | Subtract and assignment operator. It subtracts right operand from the left operand and assign the result to left operand. | C -= A is equivalent to C = C - A |
| *= | Multiply and assignment operator. It multiplies right operand with the left operand and assign the result to left operand. | C *= A is equivalent to C = C * A |
| /= | Divide and assignment operator. It divides left operand with the right operand and assign the result to left operand. | C /= A, C and A are of same type, then it is equivalent to C = C / A |
| (%)= | Modulus and assignment operator. It takes modulus using two operands and assign the result to left operand. | C %= A is equivalent to C = C % A |
| <<= | Left shift and assignment operator. | C <<= 2 is same as C = C << 2 |
| >>= | Right shift and assignment operator. | C >>= 2 is same as C = C >> 2 |
| &= | Bitwise AND and assignment operator. | C &= 2 is same as C = C & 2 |
| ^= | Bitwise XOR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
| |= | Bitwise OR and assignment operator. | C |= 2 is same as C = C | 2 |
Example
The following simple example program demonstrates the assignment operators. Copy and paste the following Java program and save it as Test.java file, then compile and run this program:
Test.java File Code:
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println("c = a + b = " + c );
c += a ;
System.out.println("c += a = " + c );
c -= a ;
System.out.println("c -= a = " + c );
c *= a ;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
System.out.println("c /= a = " + c );
a = 10;
c = 15;
c %= a ;
System.out.println("c %= a = " + c );
c <<= 2 ;
System.out.println("c <>= 2 ;
System.out.println("c >>= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
c &= a ;
System.out.println("c &= a = " + c );
c ^= a ;
System.out.println("c ^= a = " + c );
c |= a ;
System.out.println("c |= a = " + c );
}
}
This will produce the following result:
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <>= 2 = 5
c >>= 2 = 1
c &= a = 0
c ^= a = 10
c |= a = 10
Conditional Operator (?:)
The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable.
variable x = (expression) ? value if true : value if false
Example
Test.java File Code:
public class Test {
public static void main(String[] args) {
int a , b;
a = 10;
b = (a == 1) ? 20 : 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20 : 30;
System.out.println( "Value of b is : " + b );
}
}
This will produce the following result:
Value of b is : 30
Value of b is : 20
instanceof Operator
This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type).
The instanceof operator is used as follows:
(Object reference variable) instanceof (class/interface type)
If the object referred to by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result is true.
Here is one example:
String name = "James";
boolean result = name instanceof String; // Since name is of String type, result is true
This operator will still return true if the object being compared is compatible with the type on the right.
Here is one more example:
class Vehicle {}
public class Car extends Vehicle {
public static void main(String[] args) {
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println(result);
}
}
This will produce the following result:
true
Java Operator Precedence
When multiple operators appear in an expression, the order of evaluation depends on their precedence. In an expression with multiple operators, the different precedence levels can lead to vastly different results.
For example, (1+3) + (3+2)*2, if addition is evaluated first, the answer is 18; if multiplication is evaluated first, the answer is 14.
Another example: x = 7 + 3 * 2; here x gets 13, not 20, because the multiplication operator * has higher precedence than the addition operator +, so 3 * 2 gets 6 first, then 7 is added.
The operator with the highest precedence is at the top of the table, and the one with the lowest is at the bottom.
| Category | Operator | Associativity |
|---|---|---|
| Postfix | () [] . (dot operator) | Left to right |
| Unary | expr++ expr-- | Left to right |
| Unary | ++expr --expr + - ~ ! | Right to left |
| Multiplicative | * / % | Left to right |
| Additive | + - | Left to right |
| Shift | >> >>> << | Left to right |
| Relational | > >= < <= | Left to right |
| Equality | == != | Left to right |
| Bitwise AND | & | Left to right |
| Bitwise XOR | ^ | Left to right |
| Bitwise OR | | | Left to right |
| Logical AND | && | Left to right |
| Logical OR | || | Left to right |
| Conditional | ?: | Right to left |
| Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
| Comma | , | Left to right |
YouTip