Operators in Java

Operators in Java are symbols that perform specific operations on one or more operands (variables or values). They are used to manipulate and perform calculations on data. Java provides a wide range of operators categorized into different types, such as arithmetic, assignment, comparison, logical, bitwise, and more. In this blog, we will explore each category of operators with examples to understand their functionality.

Arithmetic Operators:

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.

Example:
int a = 10;
int b = 5;
int sum = a + b;        // Addition
int difference = a - b; // Subtraction
int product = a * b;    // Multiplication
int quotient = a / b;   // Division
int remainder = a % b;  // Modulus
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations and assign the result back to the variable.

Example
int a = 10;
int b = 5;
a += b; // Equivalent to a = a + b
System.out.println("a: " + a);  // Output: 15
a *= b; // Equivalent to a = a * b
System.out.println("a: " + a);  // Output: 75
a %= b; // Equivalent to a = a % b
System.out.println("a: " + a);  // Output: 0
Comparison Operators:

Comparison operators are used to compare two values and evaluate the result as either true or false.

Example
int a = 10;
int b = 5;
boolean isEqual = (a == b);  // Equal to
boolean isNotEqual = (a != b);  // Not equal to
boolean isGreater = (a > b);  // Greater than
boolean isLess = (a < b);  // Less than
boolean isGreaterOrEqual = (a >= b);  // Greater than or equal to
boolean isLessOrEqual = (a <= b);  // Less than or equal to
System.out.println("Is Equal: " + isEqual);
System.out.println("Is Not Equal: " + isNotEqual);
System.out.println("Is Greater: " + isGreater);
System.out.println("Is Less: " + isLess);
System.out.println("Is Greater or Equal: " + isGreaterOrEqual);
System.out.println("Is Less or Equal: " + isLessOrEqual);
Logical Operators

Logical operators are used to perform logical operations on boolean values. They combine multiple conditions and evaluate the overall result.

Example
boolean condition1 = true;
boolean condition2 = false;
boolean result1 = condition1 && condition2;  // Logical AND
boolean result2 = condition1 || condition2;  // Logical OR
boolean result3 = !condition1;  // Logical NOT
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
Bitwise Operators

Bitwise operators are used to perform operations at the individual bit level of integers.

Example
int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
int bitwiseAnd = a & b;  // Bitwise AND
int bitwiseOr = a | b;  // Bitwise OR
int bitwiseXor = a ^ b;  // Bitwise XOR
int bitwiseNotA = ~a;  // Bitwise NOT for a
int leftShift = a << 1;  // Left shift by 1 position
int rightShift = a >> 1;  // Right shift by 1 position
System.out.println("Bitwise AND: " + bitwiseAnd);
System.out.println("Bitwise OR: " + bitwiseOr);
System.out.println("Bitwise XOR: " + bitwiseXor);
System.out.println("Bitwise NOT for A: " + bitwiseNotA);
System.out.println("Left Shift: " + leftShift);
System.out.println("Right Shift: " + rightShift);

These are just a few examples of the operators available in Java. Understanding and utilizing operators is crucial for performing various operations and calculations in Java programming.