Java - Operators

Java operators are symbols used to perform operations on operands. Operators can be classified into different categories based on the type of operation they perform. The categories of operators are:

  • Arithmetic Operators: These operators are used to perform arithmetic operations on numeric values. Examples of arithmetic operators are +, -, *, /, and %.
  • Assignment Operators: These operators are used to assign values to variables. Examples of assignment operators are =, +=, -=, *=, and /=.
  • Comparison Operators: These operators are used to compare two values and return a Boolean value. Examples of comparison operators are ==, !=, >, <, >=, and <=.
  • Logical Operators: These operators are used to perform logical operations on Boolean values. Examples of logical operators are &&, ||, and !.
  • Bitwise Operators: These operators are used to perform operations on individual bits of a value. Examples of bitwise operators are &, |, ^, <<, and >>.

Now, let's take a look at each category of operators in more detail:

Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations on numeric values. Here are some examples:

int a = 5;
int b = 2;
int sum = a + b; // sum is 7
int difference = a - b; // difference is 3
int product = a * b; // product is 10
int quotient = a / b; // quotient is 2
int remainder = a % b; // remainder is 1

In the example above, we declare two integer variables a and b. We then perform various arithmetic operations on these variables using the arithmetic operators.

Assignment Operators:

Assignment operators are used to assign values to variables. Here are some examples:

int a = 5;
a += 2; // a is now 7
a -= 2; // a is now 5
a *= 2; // a is now 10
a /= 2; // a is now 5

In the example above, we declare an integer variable a and assign it a value of 5. We then use various assignment operators to modify the value of a.

Comparison Operators:

Comparison operators are used to compare two values and return a Boolean value. Here are some examples:

int a = 5;
int b = 2;
boolean isEqual = (a == b); // isEqual is false
boolean isNotEqual = (a != b); // isNotEqual is true
boolean isGreater = (a > b); // isGreater is true
boolean isLess = (a < b); // isLess is false
boolean isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true
boolean isLessOrEqual = (a <= b); // isLessOrEqual is false

In the example above, we declare two integer variables a and b. We then use various comparison operators to compare the values of a and b and store the result in Boolean variables.

Logical Operators:

Logical operators are used to perform logical operations on Boolean values. Here are some examples:

boolean a = true;
boolean b = false;
boolean andResult = (a && b); // andResult is false
boolean orResult = (a || b); // orResult is true
boolean notResult = !a; // notResult is false

In the example above, we declare two Boolean variables a and b. We then use various logical operators to perform logical operations on these variables and store the result in Boolean variables.

Bitwise Operators: 

int a = 5; // binary representation is 0101
int b = 3; // binary representation is 0011
int c = a & b; // bitwise AND operation, result is 0001 (1 in decimal)
int d = a | b; // bitwise OR operation, result is 0111 (7 in decimal)
int e = a ^ b; // bitwise XOR operation, result is 0110 (6 in decimal)
int f = ~a; // bitwise complement operation, result is 11111111111111111111111111111010 (-6 in decimal)
int g = a << 2; // left shift operation, result is 010100 (20 in decimal)
int h = b >> 1; // right shift operation, result is 0001 (1 in decimal)