Module 4 of 5 · Java Fundamentals · Beginner

Operators and Expressions

Duration: 5 min

What are Operators?

Operators are symbols that perform operations on variables and values. Java has several categories of operators that allow you to manipulate data and make decisions in your programs.

Arithmetic Operators

Arithmetic operators perform mathematical calculations:
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus - remainder after division)
• ++ (Increment)
• -- (Decrement)

public class ArithmeticOperators {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        
        System.out.println("Addition: " + (a + b));      // 13
        System.out.println("Subtraction: " + (a - b));   // 7
        System.out.println("Multiplication: " + (a * b)); // 30
        System.out.println("Division: " + (a / b));      // 3
        System.out.println("Modulus: " + (a % b));       // 1
        
        int x = 5;
        System.out.println("Before increment: " + x);
        x++;
        System.out.println("After increment: " + x);     // 6
    }
}
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Before increment: 5
After increment: 6

Comparison Operators

Comparison operators compare two values and return a boolean (true or false):
• == (Equal to)
• != (Not equal to)
• > (Greater than)
• < (Less than)
• >= (Greater than or equal to)
• <= (Less than or equal to)

public class ComparisonOperators {
    public static void main(String[] args) {
        int age = 20;
        
        System.out.println("age == 20: " + (age == 20));    // true
        System.out.println("age != 20: " + (age != 20));    // false
        System.out.println("age > 18: " + (age > 18));      // true
        System.out.println("age < 25: " + (age < 25));      // true
        System.out.println("age >= 20: " + (age >= 20));    // true
        System.out.println("age <= 19: " + (age <= 19));    // false
    }
}
age == 20: true
age != 20: false
age > 18: true
age < 25: true
age >= 20: true
age <= 19: false

Logical Operators

Logical operators combine boolean values:
• && (AND - both conditions must be true)
• || (OR - at least one condition must be true)
• ! (NOT - reverses the boolean value)

public class LogicalOperators {
    public static void main(String[] args) {
        int age = 25;
        boolean hasLicense = true;
        
        // AND operator
        if (age >= 18 && hasLicense) {
            System.out.println("Can drive");
        }
        
        // OR operator
        boolean isWeekend = false;
        boolean isHoliday = true;
        if (isWeekend || isHoliday) {
            System.out.println("No work today!");
        }
        
        // NOT operator
        if (!isWeekend) {
            System.out.println("It's a weekday");
        }
    }
}
Can drive
No work today!
It's a weekday

Assignment Operators

Assignment operators assign values to variables:
• = (Assign)
• += (Add and assign)
• -= (Subtract and assign)
• *= (Multiply and assign)
• /= (Divide and assign)

public class AssignmentOperators {
    public static void main(String[] args) {
        int x = 10;
        System.out.println("x = " + x);
        
        x += 5;  // x = x + 5
        System.out.println("After += 5: " + x);  // 15
        
        x -= 3;  // x = x - 3
        System.out.println("After -= 3: " + x);  // 12
        
        x *= 2;  // x = x * 2
        System.out.println("After *= 2: " + x);  // 24
    }
}
x = 10
After += 5: 15
After -= 3: 12
After *= 2: 24

💡 Tip: Operator precedence matters! Multiplication and division are performed before addition and subtraction. Use parentheses to make your intent clear.

Learn more: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

❓ What is the result of 10 % 3?

❓ What is a best practice when working with Operators and Expressions?

💡 Tip: Pro Tip: Master Operators and Expressions thoroughly. This foundation is crucial for writing professional Java code.

← Previous Continue interactively → Next →

Related Courses