Day 5: Understanding Java Operators
Source: Dev.to
What is an Operator in Java?
In Java, an operator is a special symbol or keyword used to perform operations such as:
- Addition
- Subtraction
- Division
- Comparison
- Logical checking
Important terms
- Operator – Symbol that performs an operation
- Operand – Value on which the operator works
Example
int a = 10;
=is the operatoraand10are operands
Types of Operators in Java
Assignment Operator
Used to assign a value to a variable.
int no = 10;
Arithmetic Operators
Used to perform mathematical calculations.
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
Examples
int a = 10;
int b = 3;
System.out.println(a + b); // Addition
System.out.println(a - b); // Subtraction
System.out.println(a * b); // Multiplication
System.out.println(a / b); // Division
System.out.println(a % b); // Modulus
Unary Operators
Unary operators work on a single variable.
Increment (++)
-
Post‑increment (
no++) – Use the value first, then increase it.int no = 10; System.out.println(no++); // 10 System.out.println(no); // 11 -
Pre‑increment (
++no) – Increase the value first, then use it.int age = 15; System.out.println(++age); // 16
Decrement (--)
-
Post‑decrement (
no--) – Use the value first, then decrease it.int no = 10; System.out.println(no--); // 10 System.out.println(no); // 9 -
Pre‑decrement (
--no) – Decrease the value first, then use it.int age = 15; System.out.println(--age); // 14
Equality and Relational Operators
Used to compare two values.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
| “ | Greater than |
= | Greater than or equal to |
Examples
int a = 10;
int b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a 18 && age < 60); // true