9 Operators in Java: A Comprehensive Guide

9 Operators in Java: A Comprehensive Guide

Operators in Java

Operators in Java are special symbols that perform specific operations on one, two, or three operands and then return a result. Java supports a variety of operators to perform different types of operations, such as arithmetic, logical, bitwise, relational, and more.

Types of Operators in Java

Java provides several categories of operators:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Unary Operators
  7. Ternary Operator
  8. instanceof Operator
  9. Shift Operators

Let’s go through each type with examples.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder). Read more here.

Example:

public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 10, b = 5;
        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Modulus: " + (a % b));
    }
}

Relational (Comparison) Operators

These operators compare two values and return a boolean result (true or false). Read more here.

Example:

public class RelationalExample {
    public static void main(String[] args) {
        int x = 10, y = 20;
        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));
    }
}

Logical Operators

Logical operators are used to perform logical operations such as AND, OR, and NOT. Read more here.

Example:

public class LogicalExample {
    public static void main(String[] args) {
        boolean a = true, b = false;
        System.out.println("a && b: " + (a && b));
        System.out.println("a || b: " + (a || b));
        System.out.println("!a: " + (!a));
    }
}

Bitwise Operators

Bitwise operators perform operations at the bit level.

Example:

public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3;
        System.out.println("a & b: " + (a & b)); // AND
        System.out.println("a | b: " + (a | b)); // OR
        System.out.println("a ^ b: " + (a ^ b)); // XOR
        System.out.println("~a: " + (~a)); // Complement
    }
}

Assignment Operators

Assignment operators are used to assign values to variables.

Example:

public class AssignmentExample {
    public static void main(String[] args) {
        int a = 10;
        a += 5; // a = a + 5
        System.out.println("a += 5: " + a);
        a -= 2; // a = a - 2
        System.out.println("a -= 2: " + a);
        a *= 3; // a = a * 3
        System.out.println("a *= 3: " + a);
        a /= 2; // a = a / 2
        System.out.println("a /= 2: " + a);
    }
}

Unary Operators

Unary operators work with a single operand.

Example:

public class UnaryExample {
    public static void main(String[] args) {
        int a = 10;
        System.out.println("++a: " + (++a)); // Pre-increment
        System.out.println("a++: " + (a++)); // Post-increment
        System.out.println("--a: " + (--a)); // Pre-decrement
        System.out.println("a--: " + (a--)); // Post-decrement
    }
}

Ternary Operator

The ternary operator is a shorthand for an if-else statement.

Example:

public class TernaryExample {
    public static void main(String[] args) {
        int a = 10, b = 20;
        int min = (a < b) ? a : b;
        System.out.println("Minimum value: " + min);
    }
}

instanceof Operator

The instanceof operator checks whether an object is an instance of a specific class.

Example:

class Animal {}
class Dog extends Animal {}

public class InstanceofExample {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println("a instanceof Dog: " + (a instanceof Dog));
        System.out.println("a instanceof Animal: " + (a instanceof Animal));
    }
}

Shift Operators

Shift operators are used to shift bits left or right.

Example:

public class ShiftOperators {
    public static void main(String[] args) {
        int x = 8; // Binary: 1000
        System.out.println(x << 2); // 32 (Left shift by 2: 100000)
        System.out.println(x >> 2); // 2 (Right shift by 2: 10)
    }
}

Comparison with Other Languages

Java shares many operators with other languages like C, C++, and Python, but there are key differences:

Python vs. Java

  1. Python uses and, or, not for logical operations instead of &&, ||, !.
  2. Python’s division / always returns a float, whereas Java performs integer division if both operands are integers.

C/C++ vs. Java

  1. Java does not have operator overloading, unlike C++.
  2. Java does not have a pointer dereference operator (* for pointers) like C/C++.

JavaScript vs. Java

  1. JavaScript uses === for strict equality, whereas Java uses == for primitive comparisons.
  2. JavaScript has typeof instead of instanceof for type checking.

Understanding these differences can help developers working across multiple languages.

Conclusion

Operators in Java provide essential functionality for performing computations and logical operations. Understanding these operators and their use cases will help you write efficient and readable Java programs. Whether you’re dealing with simple arithmetic or complex bitwise operations, Java offers a wide range of operators to suit your needs.

If you like this post, please comment and share. For more such articles, visit my blog.

Share This Story, Choose Your Platform!

About the author : Mohit Jain

gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==

I have more than five years of work experience as a Java Developer. I am passionate about teaching and learning new technologies. I specialize in Java 8, Hibernate, Spring Framework, Spring Boot, and databases like MySQL, and Oracle. I like to share my knowledge with others in the form of articles.

Leave A Comment