Lesson 6 – Operators

Operators allow you to perform actions on variables and values. Java provides several types of operators, such as:


1. Arithmetic Operators

These operators perform basic mathematical calculations.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)

Example 1 – Full Program

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("Modulus: " + (a % b));
    }
}
        

2. Assignment Operators

OperatorMeaning
=Assign value
+=Add then assign
-=Subtract then assign
*=Multiply then assign
/=Divide then assign

Example 2 – Full Program

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

        x += 5;   // x = x + 5
        x -= 3;   // x = x - 3
        x *= 2;   // x = x * 2
        x /= 4;   // x = x / 4

        System.out.println("Final value: " + x);
    }
}
        

3. Comparison Operators

Used to compare two values. Result is true or false.

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Example 3 – Full Program

public class Main {
    public static void main(String[] args) {
        int a = 10, 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 >= b);
        System.out.println(a <= b);
    }
}
        

4. Logical Operators

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT

Example 4 – Full Program

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

        System.out.println(a && b);  // false
        System.out.println(a || b);  // true
        System.out.println(!a);      // false
    }
}
        

5. Increment and Decrement

Increase or decrease value by 1.

OperatorMeaning
++Increment
--Decrement

Example 5 – Full Program

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

        x++;   // 6
        x--;   // 5

        System.out.println("Final value: " + x);
    }
}
        

6. Real-Life Example – Simple Calculator

Example 6 – Full Program

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter first number: ");
        double a = input.nextDouble();

        System.out.print("Enter second number: ");
        double b = input.nextDouble();

        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
    }
}
        

Summary

Back to Top