Lesson 15 of 50 – Java Operators
30%

Java Operators

Operators are special symbols used to perform operations on variables and values. Java supports arithmetic, relational, logical, assignment, unary, bitwise, and other operators.

1. What Are Operators in Java?

Operators perform operations such as addition, subtraction, comparison, and logical checking.

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        System.out.println(a + b);
        System.out.println(a > b);
    }
}
Output:
15
true

Here, + adds two numbers, while > checks whether the first number is greater than the second.

2. Arithmetic Operators

Arithmetic operators perform mathematical calculations.

Operator Operation Example Result
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Remainder10 % 31
public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;

        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("Remainder: " + (a % b));
    }
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1
Important: When both operands are integers, Java performs integer division. Therefore, 10 / 3 gives 3, not 3.333....
3. Assignment Operators

Assignment operators assign values to variables or update their existing values.

Operator Example Equivalent operation
=a = 10Assign 10 to a
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 2a = a * 2
/=a /= 2a = a / 2
%=a %= 3a = a % 3
public class Main {
    public static void main(String[] args) {
        int a = 10;

        a += 5;
        System.out.println(a);

        a -= 3;
        System.out.println(a);

        a *= 2;
        System.out.println(a);

        a /= 4;
        System.out.println(a);
    }
}
Output:
15
12
24
6
4. Relational Operators

Relational operators compare two values. Their expressions produce either true or false.

Operator Meaning Example
==Equal to10 == 10 → true
!=Not equal to10 != 5 → true
>Greater than10 > 5 → true
<Less than5 < 10 → true
>=Greater than or equal to10 >= 10 → true
<=Less than or equal to5 <= 10 → true
public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        System.out.println(a == b);
        System.out.println(a != b);
        System.out.println(a > b);
        System.out.println(a < b);
        System.out.println(a >= 10);
        System.out.println(b <= 5);
    }
}
Output:
false
true
true
false
true
true
Do not confuse = with ==. The first assigns a value; the second compares values.
5. Logical Operators

Logical operators combine or reverse Boolean expressions. They are commonly used in conditions.

Operator Name Meaning
&& Logical AND True when both conditions are true
|| Logical OR True when at least one condition is true
! Logical NOT Reverses a Boolean value
public class Main {
    public static void main(String[] args) {
        int age = 20;
        boolean hasID = true;

        System.out.println(age >= 18 && hasID);
        System.out.println(age < 18 || hasID);
        System.out.println(!hasID);
    }
}
Output:
true
true
false
Truth Table for AND and OR
A B A && B A || B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse
Example: A student can enter an exam when their attendance is sufficient AND their admit card is available. Both conditions must be true.
6. Unary Operators

Unary operators work with one operand. Examples include increment, decrement, unary plus, unary minus, and logical NOT.

Operator Meaning
++Increment by one
--Decrement by one
+Unary plus
-Unary minus
!Logical NOT
public class Main {
    public static void main(String[] args) {
        int a = 5;

        a++;
        System.out.println(a);

        a--;
        System.out.println(a);

        System.out.println(-a);
        System.out.println(!true);
    }
}
Output:
6
5
-5
false
7. Increment and Decrement: ++ and --

The increment operator increases a variable by one, while the decrement operator decreases it by one.

Post-increment
int a = 5;
System.out.println(a++);
System.out.println(a);
Output:
5
6

With a++, the current value is used in the expression first, and then the variable is increased.

Pre-increment
int b = 5;
System.out.println(++b);
System.out.println(b);
Output:
6
6

With ++b, the variable is increased first, and then the updated value is used.

8. Ternary Operator

The ternary operator is a short form of an if-else expression.

Syntax:

condition ? valueIfTrue : valueIfFalse;
public class Main {
    public static void main(String[] args) {
        int age = 20;

        String result = (age >= 18)
                ? "Eligible to vote"
                : "Not eligible to vote";

        System.out.println(result);
    }
}
Output:
Eligible to vote

If the condition is true, the first value is selected. Otherwise, the second value is selected.

9. Bitwise Operators

Bitwise operators perform operations on the individual bits of integer values.

Operator Name
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Left shift
>>Signed right shift
>>>Unsigned right shift
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;

        System.out.println(a & b);
        System.out.println(a | b);
        System.out.println(a ^ b);
        System.out.println(a << 1);
    }
}
Output:
1
7
6
10

For positive integers, 5 is 0101 and 3 is 0011 in four-bit binary notation. Bitwise AND keeps only the bits that are set in both numbers.

10. The instanceof Operator

The instanceof operator checks whether an object is an instance of a particular class or a compatible type.

public class Main {
    public static void main(String[] args) {
        String name = "Rahul";

        System.out.println(name instanceof String);
    }
}
Output:
true

The variable name refers to a String object, so the result is true.

11. Operator Precedence

Operator precedence determines which operation is evaluated first in an expression.

public class Main {
    public static void main(String[] args) {
        int result1 = 10 + 5 * 2;
        int result2 = (10 + 5) * 2;

        System.out.println(result1);
        System.out.println(result2);
    }
}
Output:
20
30

In the first expression, multiplication is performed before addition. In the second expression, parentheses make addition happen first.

Basic precedence order
  1. Parentheses: ()
  2. Unary operators: ++, --, !
  3. Multiplication, division, remainder: * / %
  4. Addition and subtraction: + -
  5. Relational comparisons: < > <= >=
  6. Equality: == !=
  7. Logical AND: &&
  8. Logical OR: ||
  9. Assignment: =, +=, etc.
Use parentheses when an expression is difficult to read. They make your intended order clearer.
12. Short-Circuit Evaluation

Java's logical AND (&&) and logical OR (||) operators use short-circuit evaluation.

  • For &&, if the first condition is false, Java does not evaluate the second one.
  • For ||, if the first condition is true, Java does not evaluate the second one.
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 0;

        boolean result = (a < 0) && (10 / b > 1);

        System.out.println(result);
    }
}
Output:
false

The first condition is false, so Java skips the second condition. This prevents division by zero in this example.

Short-circuit evaluation works with && and ||. The Boolean operators & and | do not short-circuit when used with Boolean operands.
13. Practical Example: Student Eligibility

This program uses arithmetic, relational, logical, and ternary operators to check a student's result.

public class Main {
    public static void main(String[] args) {
        int english = 75;
        int computer = 85;
        int mathematics = 65;

        int total = english + computer + mathematics;
        double average = total / 3.0;

        boolean passed =
                english >= 40 &&
                computer >= 40 &&
                mathematics >= 40;

        String result = passed ? "Pass" : "Fail";

        System.out.println("Total Marks: " + total);
        System.out.printf("Average: %.2f%n", average);
        System.out.println("Result: " + result);
    }
}
Output:
Total Marks: 225
Average: 75.00
Result: Pass
Lesson Summary:
  • Arithmetic operators perform calculations.
  • Assignment operators store or update values.
  • Relational operators compare values.
  • Logical operators combine Boolean conditions.
  • Unary operators include increment and decrement.
  • The ternary operator provides a short conditional expression.
  • Bitwise operators work with individual bits.
  • Operator precedence determines evaluation order.

🧠 Quick Quiz

Q1. What is the result of 10 % 3?



Q2. Which operator means logical AND?



Q3. What is the result of 10 + 5 * 2?