Skip to main content

Operators

  • Operators are special symbols in Java that perform specific operations on one, two, or three operands, and then return a result.
  • Operands are the values or variables involved in the operation.

Types of Operators in Java​

Java provides a rich set of operators to manipulate variables. These operators can be categorized into the following groups:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Unary Operators
  6. Bitwise Operators
  7. Ternary Operator

1. Arithmetic Operators​

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example:

int a = 10;
int b = 5;
int sum = a + b; // sum is 15
int difference = a - b; // difference is 5
int product = a * b; // product is 50
int quotient = a / b; // quotient is 2
int remainder = a % b; // remainder is 0

2. Relational Operators​

Relational operators compare two values and return a boolean result (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

int a = 10;
int b = 5;
boolean result1 = a == b; // result1 is false
boolean result2 = a != b; // result2 is true
boolean result3 = a > b; // result3 is true
boolean result4 = a < b; // result4 is false
boolean result5 = a >= b; // result5 is true
boolean result6 = a <= b; // result6 is false

3. Logical Operators​

Logical operators are used to combine multiple boolean expressions.

OperatorDescriptionExample
&&Logical ANDa && b
``
!Logical NOT!a

Example:

boolean x = true;
boolean y = false;
boolean result1 = x && y; // result1 is false
boolean result2 = x || y; // result2 is true
boolean result3 = !x; // result3 is false

4. Assignment Operators​

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assignmenta = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5

Example:

int a = 10;
a += 5; // a is now 15
a -= 3; // a is now 12
a *= 2; // a is now 24
a /= 4; // a is now 6
a %= 3; // a is now 0

5. Unary Operators​

Unary operators operate on a single operand to perform various operations.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Incrementa++ or ++a
--Decrementa-- or --a
!Logical NOT!a

Example:

int a = 10;
int b = ++a; // a is 11, b is 11 (pre-increment)
int c = a--; // a is 10, c is 11 (post-decrement)
boolean d = !true; // d is false

6. Bitwise Operators​

Bitwise operators perform operations on the binary representations of integers.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise complement~a
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example:

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int c = a & b; // c is 1 (0001 in binary)
int d = a | b; // d is 7 (0111 in binary)
int e = a ^ b; // e is 6 (0110 in binary)
int f = ~a; // f is -6 (inverts all bits)
int g = a << 1; // g is 10 (1010 in binary)
int h = a >> 1; // h is 2 (0010 in binary)
int i = a >>> 1; // i is 2 (0010 in binary)

7. Ternary Operator​

The ternary operator is a shorthand for the if-else statement. It takes three operands.

OperatorDescriptionExample
? :Ternary (conditional) operatorcondition ? value1 : value2

Example:

int a = 10;
int b = 5;
int max = (a > b) ? a : b; // max is 10

Summary​

Operators in Java are symbols that perform operations on variables and values. Here’s a quick recap of what we've covered:

  1. Arithmetic Operators: +, -, *, /, %

  2. Relational Operators: ==, !=, >, <, >=, <=

  3. Logical Operators: &&, ||, !

  4. Assignment Operators: =, +=, -=, *=, /=, %=

  5. Unary Operators: +, -, ++, --, !

  6. Bitwise Operators: &, |, ^, ~, <<, >>, >>>

  7. Ternary Operator: ? :