Skip to content

Operators in Java

Operators in Java are special symbols or keywords used to perform operations on variables and values. They form the backbone of most logical and mathematical operations in Java. Below is a detailed discussion on Java operators with examples.


Types of Operators in Java

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

1. Arithmetic Operators

These operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example:

public class ArithmeticOperators {

    public static void main(String[] args) {

        int a = 10, b = 3;

        System.out.println(“Addition: ” + (a + b));       // Output: 13

        System.out.println(“Subtraction: ” + (a – b));    // Output: 7

        System.out.println(“Multiplication: ” + (a * b)); // Output: 30

        System.out.println(“Division: ” + (a / b));       // Output: 3

        System.out.println(“Modulus: ” + (a % b));        // Output: 1

    }

}


2. Relational (Comparison) Operators

These operators are used to compare two values and return a boolean result.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
Greater thana > b
Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

public class RelationalOperators {

    public static void main(String[] args) {

        int a = 10, b = 20;

        System.out.println(a == b);  // false

        System.out.println(a != b);  // true

        System.out.println(a > b);   // false

        System.out.println(a < b);   // true

        System.out.println(a >= b);  // false

        System.out.println(a <= b);  // true

    }

}


3. Logical Operators

These are used to combine multiple boolean expressions.

OperatorDescriptionExample
&&Logical ANDa > 5 && b < 10
``
!Logical NOT!(a > 5)

Example:

public class LogicalOperators {

    public static void main(String[] args) {

        int a = 10, b = 5;

        System.out.println((a > 5) && (b < 10)); // true

        System.out.println((a > 15) || (b < 10)); // true

        System.out.println(!(a > 15)); // true

    }

}


4. Bitwise Operators

Operate at the bit level.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise Complement~a
<< Left shifta << 2
>> Right shifta >> 2
>>> Unsigned right shifta >>> 2

Example:

public class BitwiseOperators {

    public static void main(String[] args) {

        int a = 5;  // 0101 in binary

        int b = 3;  // 0011 in binary

        System.out.println(“a & b: ” + (a & b)); // 0001 => 1

        System.out.println(“a | b: ” + (a | b)); // 0111 => 7

        System.out.println(“a ^ b: ” + (a ^ b)); // 0110 => 6

        System.out.println(“~a: ” + (~a));       // -6

        System.out.println(“a << 1: ” + (a << 1)); // 1010 => 10

        System.out.println(“a >> 1: ” + (a >> 1)); // 0010 => 2

    }

}


5. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Simple assignmenta = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5

6. Unary Operators

These operators require only one operand.

OperatorDescriptionExample
+Positive+a
Negative-a
++Increment++a
Decrement–a
!Logical complement (NOT)!a

Example:

public class UnaryOperators {

    public static void main(String[] args) {

        int a = 10;

        System.out.println(“Initial a: ” + a); // 10

        System.out.println(“Post-increment: ” + a++); // 10

        System.out.println(“After post-increment: ” + a); // 11

        System.out.println(“Pre-increment: ” + ++a); // 12

    }

}


7. Ternary Operator

This is a shorthand for if-else.

SyntaxDescription
condition ? value1 : value2If condition is true, returns value1; otherwise value2.

Example:

public class TernaryOperator {

    public static void main(String[] args) {

        int a = 10, b = 20;

        int max = (a > b) ? a : b;

        System.out.println(“Maximum: ” + max); // Output: 20

    }

}


8. Special Operators

  1. Instanceof Operator: Used to test whether an object is an instance of a specific class or subclass.

String s = “Hello”;

System.out.println(s instanceof String); // true

  • Type Cast Operator: Converts data types explicitly.

int x = (int) 12.34; // Cast double to int


Summary

Java operators provide a powerful way to perform calculations, logic, and bit-level manipulations. Understanding their usage is crucial for writing efficient and readable code