Java Lesson 6 - Operators | Dataplexa

Operators in Java

Programs do more than store data. They calculate, compare, and make decisions using that data.

In Java, these actions are performed using operators. Operators tell Java how to work with variables and values.


Why Operators Are Important

Almost every Java program uses operators. From simple calculations to complex business logic, operators play a key role.

Without operators, programs would not be able to process data or make meaningful decisions.


Main Types of Operators in Java

Java provides different types of operators based on the task they perform.

  • Arithmetic Operators – perform mathematical calculations
  • Relational Operators – compare values
  • Logical Operators – combine conditions
  • Assignment Operators – assign values to variables

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.

These operators are commonly used in calculations like totals, averages, and scores.

public class ArithmeticExample {

    public static void main(String[] args) {

        int a = 20;
        int b = 10;

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

Relational Operators

Relational operators compare two values and return a boolean result (true or false).

These operators are widely used in conditions and decision-making logic.

int x = 15;
int y = 20;

System.out.println(x > y);
System.out.println(x < y);
System.out.println(x == y);
System.out.println(x != y);

Logical Operators

Logical operators are used to combine multiple conditions. They help Java decide whether a group of conditions is true or false.

These are commonly used in real-world scenarios such as login validation or eligibility checks.

boolean hasID = true;
boolean hasTicket = false;

System.out.println(hasID && hasTicket);
System.out.println(hasID || hasTicket);
System.out.println(!hasTicket);

Assignment Operators

Assignment operators are used to assign values to variables. Java also provides shorthand operators to simplify calculations.

These operators make code shorter and easier to read.

int total = 10;

total += 5;
total -= 3;
total *= 2;

System.out.println(total);

Operators in Real Applications

In real applications, operators are used everywhere:

  • Calculating salaries and discounts
  • Checking login credentials
  • Validating user input
  • Comparing data values

Understanding operators helps you write logic that reflects real-world behavior.


What You Learned in This Lesson

  • Why operators are essential in Java
  • The main types of operators
  • How operators work with variables
  • How operators are used in real-world scenarios

In the next lesson, you will learn about input and output, which allows Java programs to interact with users.