Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 1.82 KB

File metadata and controls

58 lines (41 loc) · 1.82 KB

Operators

Welcome to the Operators section! In this lesson, we will learn about different types of operators in Java. Operators are special symbols that perform operations on variables and values. Let's explore the three main types of operators: Arithmetic, Comparison, and Logical operators.

Arithmetic Operators

Arithmetic operators are used to perform basic math operations like addition, subtraction, multiplication, and division.

Examples:

int a = 10;
int b = 5;

int sum = a + b; // Addition: 10 + 5 = 15
int difference = a - b; // Subtraction: 10 - 5 = 5
int product = a * b; // Multiplication: 10 * 5 = 50
int quotient = a / b; // Division: 10 / 5 = 2
int remainder = a % b; // Modulus: 10 % 5 = 0

Comparison Operators

Comparison operators are used to compare two values. They return true or false.

Examples:

int x = 10;
int y = 5;

boolean isEqual = (x == y); // Equal to: false
boolean isNotEqual = (x != y); // Not equal to: true
boolean isGreater = (x > y); // Greater than: true
boolean isLess = (x < y); // Less than: false
boolean isGreaterOrEqual = (x >= y); // Greater than or equal to: true
boolean isLessOrEqual = (x <= y); // Less than or equal to: false

Logical Operators

Logical operators are used to combine multiple conditions.

Examples:

boolean a = true;
boolean b = false;

boolean andResult = a && b; // Logical AND: false (both must be true)
boolean orResult = a || b; // Logical OR: true (one must be true)
boolean notResult = !a; // Logical NOT: false (opposite of true)

Summary

In this lesson, we learned about three types of operators in Java: Arithmetic, Comparison, and Logical operators. These operators help us perform calculations, compare values, and combine conditions in our programs.


<< Previous | Home | Next >>