Day 3: Operators in Java

Day 3: Operators in Java

ยท

3 min read

Operators are symbols that perform operations on variables and values known as operands. Java provides a variety of operators for performing mathematical, logical, conditional, and assignment operations.

Mathematical Operators

Mathematical operators are used to perform mathematical operations.

int a = 10, b = 3;

  1. Addition (+)
    int c = a + b;

  2. Subtraction (-)
    int d = a - b;

  3. Multiplication (*)
    int e = a * b;

  4. Division (/)
    float f = a / b; // f will be 3.33 (float as int division results in int)

  5. Modulus (%)
    int g = a % b; // g will be 1 (remainder after dividing a by b)

  6. Increment (++) and Decrement (--)
    a++; // a becomes 11 a--; // a becomes 10 again


Assignment Operators

Assignment operators are used to assign values to variables.

int a = 10;

  1. Simple Assignment (=)
    int b = a; // b also becomes 10

  2. Additive Assignment (+=)
    a += b; // a becomes 20

  3. Subtractive Assignment (-=)
    a -= b; // a becomes 10 again

  4. Multiplicative Assignment (*=)
    a *= b; // a becomes 30

  5. Divisive Assignment (/=)
    a /= b; // a becomes 3

  6. Modulus Assignment (%=)
    a %= b; // a becomes 1


Comparison Operators

Comparison operators compare two operands and determine their relation.

  1. Equal to (==)
    (a == b) // Returns false as a and b are not equal

  2. Not equal to (!=)
    (a != b) // Returns true

  3. Greater than (>)
    (a > b) // Returns true

  4. Less than (<)
    (a < b) // Returns false

  5. Greater than or equal to (>=)
    (a >= b) // Returns true

  6. Less than or equal to (<=)
    (a <= b) // Returns false


Logical Operators

Logical operators combine two or more conditions (boolean expressions).

  1. AND (&&)
    (a > b) && (c < d) evaluates to true if both conditions are true

  2. OR (||)
    (a > b) || (c < d) evaluates to true if any condition is true

  3. NOT (!)
    !(a < b) inverts the result, returning false if the condition is true.


Bitwise Operators

Bitwise operators perform operations on bits and bit patterns of operands.

  1. AND (&)
    int result = (a & b); // Performs bitwise AND

  2. OR (|)
    int result = (a | b); // Performs bitwise OR

  3. XOR (^)
    int result = (a ^ b); // Performs bitwise XOR

  4. NOT (~)
    int result = ~a; // Performs bitwise complement

  5. Left Shift (<<)
    a << 2; // Shifts a by two bits toward left

  6. Right Shift (>>)
    a >> 2; // Shifts a by two bits toward right

  7. Zero fill Right Shift (>>>)
    a >>> 2; // Shifts with zero fill instead of preserving sign bit


Conditional Operator

int result = (condition)? True: False;

int max = (a > b) ? a : b; // Returns the larger of the two values

To conclude, operators are an essential part of any programming language. Understanding which operators to use and when helps improve the expressiveness and performance of your Java code. Using operators effectively can also make your code more concise and reduce complexity.

Did you find this article valuable?

Support Laxman Rai by becoming a sponsor. Any amount is appreciated!

ย