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:

  1. Arithmetic operators
  2. Assignment operators
  3. Relational operators
  4. Logical operators
  5. Bit-wise operators
  6. Conditional Operators
 

1. Arithmetic Operators:

These operators are used with numeric values to perform common mathematical operations.​

Table 1: Arithmetic operators
OperatorMeaningExample
+Add two operandsx + y
Subtract right operand from the leftx – y
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y
++Increment – Increases the value of a variable by 1x++
– –Decrement – Decreases the value of a variable by 1x – –
 

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.

Table 2: Assignment operators
OperatorExampleEquivalent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = 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.

Table 3: Relational operators
OperatorMeaningExample
>Greater that – True if left operand is greater than the rightx > y
<Less that – True if left operand is less than the rightx < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= 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.

Table 4: Logical operators
OperatorMeaningExample
&&Logical AND — True if both the operands are truex && y
||Logical OR — True if either of the operands is truex || 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)

Table 5: Bit-wise operators
OperatorMeaningExample
&Bit-wise ANDx& y = 0 (0000 0000)
|Bit-wise ORx | y = 14 (0000 1110)
~Bit-wise NOT~x = -11 (1111 0101)
^Bit-wise XORx ^ y = 14 (0000 1110)
>>Bit-wise right shiftx>> 2 = 2 (0000 0010)
<<Bit-wise left shiftx<< 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