Comprehensive Guide to Java Operators with Code Examples

In this blog post, we’ll explore the different types of Java operators, their usage, and provide code examples along with their outputs.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations on numerical data types. These include addition, subtraction, multiplication, division, and modulus (remainder).

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus
  • ++ Increment by 1
  • -- Decrement by 1

Let’s take a look at each one of them:

Code Example:

public class ArithmeticOperatorsExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int result;

        // Addition
        result = a + b;
        System.out.println("Addition: " + result);

        // Subtraction
        result = a - b;
        System.out.println("Subtraction: " + result);

        // Multiplication
        result = a * b;
        System.out.println("Multiplication: " + result);

        // Division
        double divisionResult = (double) a / b;
        System.out.println("Division: " + divisionResult);

        // Modulus (Remainder)
        result = a % b;
        System.out.println("Modulus (Remainder): " + result);

        // Increment by 1 (++)
        result = a++;
        System.out.println("Increment (++): " + result);

        // Decrement by 1 (--)
        result = a--;
        System.out.println("Decrement (--): " + result);
    }
}

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus (Remainder): 1
Increment (++): 11
Decrement (--): 9

2. Relational Operators

Relational operators are used to compare two values and return a boolean result (true or false). They are commonly used in decision-making constructs such as if statements and loops. The relational operators in Java are as follows:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Code Example:

public class RelationalOperatorsExample {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;

        System.out.println("x == y: " + (x == y));
        System.out.println("x != y: " + (x != y));
        System.out.println("x > y: " + (x > y));
        System.out.println("x < y: " + (x < y));
        System.out.println("x >= y: " + (x >= y));
        System.out.println("x <= y: " + (x <= y));
    }
}

Output:

x == y: false
x != y: true
x > y: false
x < y: true
x >= y: false
x <= y: true

3. Logical Operators

Logical operators are used to combine multiple conditions and evaluate the overall result as true or false. They are commonly used in decision-making constructs to determine the flow of execution based on specific conditions. The logical operators in Java are as follows:

  • && (logical AND)
  • || (logical OR)
  • ! (logical NOT)

Code Example:

public class LogicalOperatorsExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;

        System.out.println("a && b: " + (a && b));
        System.out.println("a || b: " + (a || b));
        System.out.println("!a: " + !a);
    }
}

Output:

a && b: false
a || b: true
!a: false

4. Assignment Operators

Assignment operators are used to assigning values to variables. They are used to store the results of expressions or values into variables. The assignment operator in Java is the = symbol.

The Java compound assignments are:

  • += Addition assignment
  • -= Subtraction assignment
  • *= Multiplication assignment
  • /= Division assignment
  • %= Modulus assignment

Code Example:

public class AssignmentOperatorsExample {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
    
        // Simple assignment
        int result = x + y;
        System.out.println("Result: " + result);

        x += y; // Equivalent to x = x + y
        System.out.println("Addition assignment: " + x);
        
        x -= y; // Equivalent to: x = x - y;
        System.out.println("Subtraction assignment: " + x);

        x *= y; // Equivalent to: x = x * y;
        System.out.println("Multiplication assignment: " + x);

        x /= y; // Equivalent to: x = x / y;
        System.out.println("Division assignment: " + x);

        x %= y; // Equivalent to: x = x % y;
        System.out.println("Modulus assignment: " + x);
    }
}

Output:

Result: 15
Addition assignment: 15
Subtraction assignment: 5
Multiplication assignment: 50
Division assignment: 2
Modulus assignment: 0

5. Bitwise Operators

Bitwise operators are used to perform operations at the bit level. They are mostly used in low-level programming, such as embedded systems or binary data processing. The bitwise operators in Java are as follows:

  • & (bitwise AND)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • ~ (bitwise NOT)
  • << (left shift)
  • >> (right shift)
  • >>> (unsigned right shift)

Code Example:

public class BitwiseOperatorsExample {
    public static void main(String[] args) {
        int a = 5; // Binary representation: 0101
        int b = 3; // Binary representation: 0011
        int result;

        // Bitwise AND
        result = a & b; // Binary representation: 0001
        System.out.println("Bitwise AND: " + result);

        // Bitwise OR
        result = a | b; // Binary representation: 0111
        System.out.println("Bitwise OR: " + result);

        // Bitwise XOR
        result = a ^ b; // Binary representation: 0110
        System.out.println("Bitwise XOR: " + result);

        // Bitwise NOT
        result = ~a; // Binary representation: 1111_1111_1111_1111_1111_1111_1111_1010
        System.out.println("Bitwise NOT: " + result);

        // Left Shift
        result = a << 1; // Binary representation: 1010
        System.out.println("Left Shift: " + result);

        // Right Shift
        result = a >> 1; // Binary representation: 0010
        System.out.println("Right Shift: " + result);

        // Unsigned Right Shift
        result = a >>> 1; // Binary representation: 0010
        System.out.println("Unsigned Right Shift: " + result);
    }
}

Output:

Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left Shift: 10
Right Shift: 2
Unsigned Right Shift: 2

Conclusion

In this blog post, we covered the major types of Java operators, including arithmetic, relational, logical, assignment, and bitwise operators. We provided code examples for each type of operator and demonstrated their usage along with the corresponding output.