Java operators are useful for performing operations. So, they operate on some values (also known as operands) and produce a result. Java operators can be categorized mainly into arithmetic, relational and logical operators. In this article, I will be covering all these operators in detail.
Assignment Operator
The assignment operator is a special operator that assigns a value to a variable. It assigns the value on its right to the variable on its left. It is represented by the “=” sign.
Sample Code
double val1 = 2.5; // Line 1 System.out.println(val1); int val2 = 10+20; // Line 2 System.out.println(val2); int val3 = val2-5; //Line 3 System.out.println(val3);
- Line 1 assigns the value 5 to the variable val1
- Line 2 assigns the value 30 to the variable val2
- Line 3 subtracts the value 5 from val2 and assigns the result to val3
Output:
Arithmetic Operators
Arithmetic operators are the ones that are useful in performing mathematical operations. They operate on numeric values. They either operate on a single operand (known as unary operators) or two operands (known as binary operators). They can be further classified as basic arithmetic, compound assignment, and increment/decrement operators.
Basic Arithmetic
Basic arithmetic operators are those that can be used to perform the mathematical operations of addition, subtraction, multiplication, and division.
Operator | Description |
+ | Performs addition |
– | Performs subtraction |
* | Performs multiplication |
/ | Performs division |
% | Returns the remainder of the division |
Sample Code
int val1 = 200; //Line 1 int val2 = 100; //Line 2 int result=val1+val2; //Line 3 System.out.println(result); //Line 4 result=val1-val2; //Line 5 System.out.println(result); //Line 6 result=val1*val2; //Line 7 System.out.println(result); //Line 8 result=val1/val2; //Line 9 System.out.println(result); //Line 10 result=val1%val2; //Line 11 System.out.println(result); //Line 12
- Lines 1-2 create variables val1 and val2 and initialize them to 200 and 100 respectively
- Lines 3 performs addition on val1 and val2 and assigns the sum to result
- Line 4 prints result
- Similarly, Lines 5-12 perform the other arithmetic operations and print the result
Output:
300
100
20000
2
0
Compound Assignment
The compound assignment operators combine operation with an assignment. So, they are basically shortcutting operators that help to perform an operation and assignment in a single statement.
Operator | Description |
+= | Combines addition with the assignment. So a += 10 is the same as a=a+10 |
-= | Combines subtraction with the assignment. So a -= 10 is the same as a=a-10 |
*= | Combines multiplication with the assignment. So a *= 10 is the same as a=a*10 |
/= | Combines division with the assignment. So a /= 10 is the same as a=a/10 |
%= | Combines modulus with the assignment. So a %= 10 is the same as a=a%10 |
Sample Code
int val=5; //Line 1 val+=10; //Line 2 System.out.println(val); //Line 3 val=20; //Line 4 val-=5; //Line 5 System.out.println(val); //Line 6 val=4; //Line 7 val*=3; //Line 8 System.out.println(val); //Line 9 val=40; //Line 10 val/=4; //Line 11 System.out.println(val); //Line 12 val=10; //Line 13 val%=7; //Line 14 System.out.println(val); //Line 15
- Lines 1 creates a variable val with the value 5
- Line 2 adds 10 to val via the addition assignment operator
- Line 3 prints val
- Similarly, Lines 4-15, initialize val with some value, perform each of the compound assignments, and print the results.
Output:
15
15
12
10
3
Increment/Decrement Operators
The increment and decrement operators are unary operators that operate on a single operand. The increment/decrement the operand by 1.
Operator | Description |
++ | Increments its operand by 1 |
— | Decrements its operand by 1 |
Sample Code
int value1 = 8; //Line 1 int value2 = ++value1; //Line 2 System.out.println(value2); //Line 3 value1=15; //Line 4 value2 = --value1;//Line 5 System.out.println(value2);//Line 6
- Line1 declares a variable value1 and initializes it to 8
- Line 2 increments value1 via the increment operator and assign the result to value2
- Line 3 prints value2
- Similarly, Lines 4-6 use the decrement operator to decrement value1 and assign the result to value2
Output:
9
14
There are two variants of the increment/decrement operators, prefix and postfix. In the prefix notation, the operator precedes the operand. It performs the operation first and then assigns the value. In the postfix notation, the operator succeeds the operand. It assigns the value first and then performs the operation. The code above uses the prefix notation.
The following code demonstrates the postfix operation:
int value1 = 8; //Line 1 int value2 = value1++; //Line 2 System.out.println(value1); //Line 3 System.out.println(value2); //Line 4
- Line1 declares a variable value1 and initializes it to 8
- Line 2 increments value1 via the increment operator in postfix form and assign the result to value2
- Since postfix form is used, value1 is first assigned to value2 and then incremented
Output:
Relational Operators
Relational operators are used to comparing values. They return a boolean value which is the result of the comparison.
Operator | Description |
< | Performs Less Than Comparison |
<= | Performs Less Than or Equal To Comparison |
> | Performs Greater Than Comparison |
>= | Performs Greater Than or Equal To Comparison |
== | Performs Equal To Comparison |
!= | Performs Not Equal To Comparison |
Sample Code
int value1 = 50; //Line 1 int value2 = 25; //Line 2 boolean flag = value1 < value2; //Line 3 System.out.println(flag); //Line 4 flag = value1 <= value2; //Line 5 System.out.println(flag); //Line 6 flag = value1 > value2; //Line 7 System.out.println(flag); //Line 8 flag = value1 >= value2; //Line 9 System.out.println(flag); //Line 10 flag = value1 == value2; //Line 11 System.out.println(flag); //Line 12 flag = value1 != value2; //Line 13 System.out.println(flag); //Line 14
- Lines 1-2 create variables value1 and value2 and initialize them to 50 and 25 respectively
- Lines 3 performs the less than comparison on value1 and value2 and assigns the result to flag
- Line 4 prints flag
- Similarly, Lines 5-14 perform the other relational comparisons and print the result
Output:
false
false
true
true
false
true
Logical Operators
Logical operators are used to combining relational operations. They operate on boolean values and return a boolean.
Operator | Description |
|| (OR) | Returns true if any one of its operands are true, otherwise returns false |
&& (AND) | Returns true if both its operands are true, otherwise returns false |
! (NOT) | It is a unary operation. It returns the inverse of the operand. |
Sample Code
boolean bool1 = false; //Line 1 boolean bool2 = true; //Line 2 boolean flag = bool1 || bool2; //Line 3 System.out.println(flag); //Line 4 flag = bool1 && bool2; //Line 5 System.out.println(flag); //Line 6 flag = !bool1; //Line 7 System.out.println(flag); //Line 8
- Lines 1-2 create variables bool1 and bool2 and initialize them to false and true respectively
- Line 3 performs the OR operation and assigns the result to flag
- Line 4 prints flag
- Similarly, Lines 3-8 perform the other logical operations and print the result
Output:
true
false
true
Generally, logical operators are used to combining relational expressions as shown below:
int val1=15, val2=25, val3=10; if(val1 < val2 && val2 > val3){ //Line 1 // do something }
- Line 1 combines the val1<val2 and val2 > val3 expressions via the AND
- So, the code in the if block gets executed only if both conditions are true.
Ternary Operator
The ternary operator is a shortcut operator for the if-then-else statement. It assigns different values to a variable depending on a boolean condition.
Syntax
variable = condition?value1:value2
- The ternary operator checks the condition before the ? symbol
- If it is true, it assigns value1 to the variable
If it is false, it assigns value2 to the variable
Sample Code
int val1 = 100; //Line 1 int val2 = val1 > 75?20:30; //Line 2 System.out.println(val2);
- Line 1 declares a variable val1 and initializes it to 100
- Line 2 uses the ternary operator to initialize val2
- The ternary operator checks the condition val1 > 75
- Since it is true, it assigns the value 20 to val2
Output:
20
Conclusion
So, just to summarise, Java supports a wide variety of operators for performing different operations. Java operators can be classified into arithmetic, relational and logical operators. Arithmetic operators help to perform the mathematical operations, that is addition, subtraction, multiplication, and division. The relational operators help to compare values while the logical operators help to combine relational expressions.