Lesson 2.5: Operators in Java
Introduction
Operators are one of the most important concepts in programming. They are used to perform different types of operations on variables and values, such as addition, comparison, logical operations, and assignment.
For example, when you calculate the total marks of a student, compare two numbers, or check whether a student has passed an exam, you use operators.
Java provides a rich set of operators that make programming easier and more efficient.
In this lesson, you will learn the different types of operators available in Java and how to use them in real-world programs.
Learning Objectives
After completing this lesson, you will be able to:
-
Understand what operators are.
-
Use arithmetic operators.
-
Use assignment operators.
-
Use comparison (relational) operators.
-
Use logical operators.
-
Use unary operators.
-
Use increment and decrement operators.
-
Write Java programs using different operators.
What is an Operator?
An Operator is a special symbol that performs an operation on one or more operands (values or variables).
Example
int a = 10;
int b = 5;
int sum = a + b;
Here:
-
aandbare operands. -
+is the operator. -
sumstores the result.
Types of Operators in Java
Java provides the following types of operators:
-
Arithmetic Operators
-
Assignment Operators
-
Relational (Comparison) Operators
-
Logical Operators
-
Unary Operators
-
Increment and Decrement Operators
-
Ternary Operator
-
Bitwise Operators (Advanced)
In this lesson, we will focus on the most commonly used operators.
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (Remainder) | a % b |
Example Program
public class ArithmeticDemo { public static void main(String[] args) { int a = 20; int b = 6; 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 = 26
Subtraction = 14
Multiplication = 120
Division = 3
Remainder = 2
2. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Meaning |
|---|---|
| = | Assign |
| += | Add and Assign |
| -= | Subtract and Assign |
| *= | Multiply and Assign |
| /= | Divide and Assign |
| %= | Modulus and Assign |
Example
int x = 10; x += 5; System.out.println(x);
Output
15
3. Relational (Comparison) Operators
These operators compare two values and return either true or false.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not Equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or Equal to |
| <= | Less than or Equal to |
Example
int a = 10; int b = 20; System.out.println(a < b); System.out.println(a == b);
Output
true
false
4. Logical Operators
Logical operators are used to combine two or more conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Example
int age = 20; System.out.println(age >= 18 && age <= 60);
Output
true
5. Unary Operators
Unary operators work with a single operand.
| Operator | Description |
|---|---|
| + | Positive |
| – | Negative |
| ! | Logical NOT |
Example
boolean result = true; System.out.println(!result);
Output
false
6. Increment and Decrement Operators
These operators increase or decrease a variable by one.
Increment (++)
int x = 5; x++; System.out.println(x);
Output
6
Decrement (–)
int y = 10; y--; System.out.println(y);
Output
9
Pre-Increment vs Post-Increment
Pre-Increment
int a = 5; System.out.println(++a);
Output
6
The value increases before printing.
Post-Increment
int a = 5; System.out.println(a++); System.out.println(a);
Output
5
6
The value increases after printing.
7. Ternary Operator
The ternary operator is a short form of the if-else statement.
Syntax
condition ? value1 : value2;
Example
int age = 20; String result = (age >= 18) ? "Eligible" : "Not Eligible"; System.out.println(result);
Output
Eligible
8. Bitwise Operators (Introduction)
Bitwise operators perform operations on binary values.
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | XOR |
| ~ | Complement |
| << | Left Shift |
| >> | Right Shift |
These operators are mainly used in advanced programming and embedded systems.
Operator Precedence
Operators are evaluated according to their priority.
For example:
int result = 10 + 5 * 2;
Output
20
Multiplication is performed before addition.
Use parentheses to control the order of evaluation.
int result = (10 + 5) * 2;
Output
30
Common Mistakes
Using = Instead of ==
Incorrect
if (a = b)
Correct
if (a == b)
Dividing by Zero
int result = 10 / 0;
This causes an ArithmeticException.
Forgetting Parentheses
Incorrect
System.out.println("Sum = " + a + b);
Correct
System.out.println("Sum = " + (a + b));
Best Practices
-
Use meaningful variable names.
-
Add spaces around operators for better readability.
-
Use parentheses when expressions become complex.
-
Avoid unnecessary calculations.
Key Points to Remember
-
Operators perform operations on variables and values.
-
Arithmetic operators are used for calculations.
-
Relational operators return
trueorfalse. -
Logical operators combine conditions.
-
Assignment operators update variable values.
-
The ternary operator is a shorthand for
if-else.
Lesson Summary
In this lesson, you learned:
-
What operators are.
-
Different types of operators in Java.
-
Arithmetic, assignment, relational, logical, unary, increment/decrement, ternary, and bitwise operators.
-
Operator precedence.
-
Common programming mistakes.
Operators are essential because almost every Java program uses them for calculations, comparisons, and decision-making.
Practice Questions
-
What is an operator?
-
What is the difference between
=and==? -
Name five arithmetic operators.
-
What is the purpose of logical operators?
-
Explain the difference between pre-increment and post-increment.
-
What is the ternary operator?
-
What is operator precedence?
-
Why should parentheses be used in complex expressions?
Programming Exercise
Write Java programs to:
-
Perform addition, subtraction, multiplication, division, and modulus.
-
Demonstrate all assignment operators.
-
Compare two numbers using relational operators.
-
Check voting eligibility using logical operators.
-
Demonstrate pre-increment and post-increment.
-
Use the ternary operator to find the larger of two numbers.
Assignment
Create a program named OperatorDemo.java that:
-
Accepts two integer values (hardcoded for now).
-
Performs all arithmetic operations.
-
Demonstrates assignment operators.
-
Compares the numbers using relational operators.
-
Uses logical operators to evaluate a condition.
-
Displays whether the first number is even or odd using the ternary operator.
Next Lesson: Lesson 2.6 – Taking User Input Using Scanner