Java Operators
In Java programming, operators are used to perform operations on operands (i.e. variables and values).
Java categorizes the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bit-wise operators
- Conditional Operators
1. Arithmetic Operators:
These operators are used with numeric values to perform common mathematical operations.
Operator | Meaning | Example |
---|---|---|
+ | Add two operands | x + y |
– | Subtract right operand from the left | x – y |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y |
++ | Increment – Increases the value of a variable by 1 | x++ |
– – | Decrement – Decreases the value of a variable by 1 | x – – |
Example 1
//ArithmeticOperatorsDemo.java
public class ArithmeticOperatorsDemo {
public static void main(String[] args) {
int a = 100;
int b = 5;
int c, d;
System.out.println("ADD: " + (a+b));
System.out.println("SUB: " + (a-b));
System.out.println("MUL: " + (a*b));
System.out.println("DIV: " + (a/b));
System.out.println("MOD: " + (a%b));
//post increment or decrement
c = b++; d = b--;
System.out.println("Post ++: " + c);
System.out.println("Post --: " + d);
//pre increment or decrement
c = ++b; d = --b;
System.out.println("Pre ++: " + c);
System.out.println("Pre --: " + d);
}
}
Output:
ADD: 105
SUB: 95
MUL: 500
DIV: 20
MOD: 0
Post ++: 5
Post --: 6
Pre ++: 6
Pre --: 5
2. Assignment Operators:
These operators are used to assign values to variables.
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Example 2
//AssignmentOperatorsDemo.java
public class AssignmentOperatorsDemo {
public static void main(String[] args) {
int x = 5;
x = x + 5; //x += 5
System.out.println("x = " + x);
x = x - 5; //x -= 5
System.out.println("x = " + x);
x = x * 5; //x *= 5
System.out.println("x = " + x);
x = x / 5; //x /= 5
System.out.println("x = " + x);
x = x % 5; //x %= 5
System.out.println("x = " + x);
}
}
Output:
x = 10
x = 5
x = 25
x = 5
x = 0
3. Relational Operators:
These operators are used to compare two values.
Operator | Meaning | Example |
---|---|---|
> | Greater that – True if left operand is greater than the right | x > y |
< | Less that – True if left operand is less than the right | x < y |
== | Equal to – True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
Example 3
//RelationalOperatorsDemo.java
public class RelationalOperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println(a>b); //true
System.out.println(a<b); //false
System.out.println(a==b); //false
System.out.println(a!=b); //true
System.out.println(a>=b); //true
System.out.println(a<=b); //false
}
}
Output:
true
false
false
true
true
false
4. Logical Operators:
These operators are used to combine conditional statements.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND — True if both the operands are true | x && y |
|| | Logical OR — True if either of the operands is true | x || y |
! | Logical NOT — True if operand is false (complements the operand) | !x |
Example 4
//LogicalOperatorsDemo.java
public class LogicalOperatorsDemo {
public static void main(String[] args) {
boolean result;
result = ((10>5) && (9<7));
System.out.println("Logical AND = " + result); //false
result = ((10>5) || (9<7));
System.out.println("Logical OR = " + result); //true
result = !(9<7);
System.out.println("Logical NOT = " + result); //true
}
}
Output:
Logical AND = false
Logical OR = true
Logical NOT = true
5. Bit-wise Operators:
These operators are used to compare binary numbers.
For example, in the table below, let x = 10 (0000 1010
in binary) and y = 4 (0000 0100
in binary)
Operator | Meaning | Example |
---|---|---|
& | Bit-wise AND | x& y = 0 (0000 0000 ) |
| | Bit-wise OR | x | y = 14 (0000 1110 ) |
~ | Bit-wise NOT | ~x = -11 (1111 0101 ) |
^ | Bit-wise XOR | x ^ y = 14 (0000 1110 ) |
>> | Bit-wise right shift | x>> 2 = 2 (0000 0010 ) |
<< | Bit-wise left shift | x<< 2 = 40 (0010 1000 ) |
Example 5
//BitwiseOperatorsDemo.java
public class BitwiseOperatorsDemo {
public static void main(String[] args) {
int x = 10;
int y = 4;
System.out.println("Bit-wise AND: " + (x & y)); //0
System.out.println("Bit-wise OR: " + (x | y)); //14
System.out.println("Bit-wise NOT: " + (~x)); //-11
System.out.println("Bit-wise XOR: " + (x ^ y)); //14
System.out.println("Bit-wise right shift: " + (x >> 2)); //2
System.out.println("Bit-wise left shift: " + (x << 2)); //40
}
}
Output:
Bit-wise AND: 0
Bit-wise OR: 14
Bit-wise NOT: -11
Bit-wise XOR: 14
Bit-wise right shift: 2
Bit-wise left shift: 40
6. Conditional Operators:
The Conditional operator is the only ternary operator (operator that takes three arguments) in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement.
Example 6
//ConditionalOperatorsDemo.java
public class ConditionalOperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 12;
boolean c = a > b ? true : false;
System.out.println("c = " + c);
}
}
Output:
c = false