Java Operators

In Java, operators are symbols or keywords used to perform operations on variables and values. They are the building blocks of Java programming and allow developers to perform tasks like arithmetic calculations, comparisons, and logical operations.

Types of Java Operators

1. Arithmetic Operators

Used to perform basic mathematical operations.

Operator
Description
Example
+
Addition
6+3 = 9
Subtraction
6-3 = 3
*
Multiplication
6*3 = 18
/
Division
6/3 = 2
%
Modulus (remainder)
7%3 = 1

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));
    }
}

2. Relational (Comparison) Operators

Used to compare two values.

Operator
Description
Example
==
Equal to
6 == 3 → false
!=
Not equal to
6 != 3 → true
>
Greater than
6 > 3 → true
<
Less than
6 < 3 → false
>=
Greater than or equal to
6 >= 3 → true
<=
Less than or equal to
6 <= 3 → false

Example:


public class ComparisonExample {
    public static void main(String[] args) {
        int x = 10, y = 20;
        System.out.println(x == y); // false
        System.out.println(x != y); // true
        System.out.println(x > y);  // false
        System.out.println(x < y);  // true
        System.out.println(x >= y); // false
        System.out.println(x <= y); // true
    }
}

3. Logical Operators

Used to combine multiple conditions.

Operator
Description
Example
&&
Logical AND (both true)
(6 > 3) && (7 > 2) → true
!
Logical NOT (negates condition)
!(6 > 3) → false

Example:


public class LogicalExample {
    public static void main(String[] args) {
        boolean condition1 = (5 > 3);
        boolean condition2 = (6 > 10);
        System.out.println(condition1 && condition2); // false
        System.out.println(condition1 || condition2); // true
        System.out.println(!condition1);             // false
    }
}

4. Assignment Operators

Used to assign values to variables.

Operator
Description
Example
=
Assign
a = 5
+=
Add and assign
a += 5 → a = a + 5
-=
Subtract and assign
a -= 5 → a = a - 5
*=
Multiply and assign
a *= 5 → a = a * 5
/=
Divide and assign
a /= 5 → a = a / 5
%=
Modulus and assign
a %= 5 → a = a % 5

Example:


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

5. Unary Operators

Operate on a single variable.

Operator
Description
Example
+
Positive
+a
-
Negative
-a
++
Increment (pre/post)
++a, a++
--
Decrement (pre/post)
--a, a--

Example:


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

6. Ternary Operator

A shorthand for an if-else condition.

Syntax:


condition ? value1 : value2

Example:


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