Operators in Java
Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read
Understand arithmetic, relational, logical, assignment, increment, decrement, and ternary operators.
Operators are special symbols used to perform actions on values and variables. They help us calculate results, compare values, update variables, and build conditions. For example, when you write `a + b`, the `+` operator adds two values. When you write `a > b`, the `>` operator compares two values. When you write `marks >= 40 && attendance >= 75`, the `&&` operator checks whether both conditions are true.
Operators are important in Java because almost every program uses them. Whether you are calculating a sum, checking even or odd, comparing two numbers, updating a counter, or writing a condition inside `if`, you are using operators.
Arithmetic Operators
Arithmetic operators are used for mathematical calculations. They work mostly with numeric values such as `int`, `long`, `float`, and `double`.
- `+` addition
- `-` subtraction
- `*` multiplication
- `/` division
- `%` remainder
The `/` operator gives the quotient. If both operands are integers, Java performs integer division and removes the decimal part. For example, `7 / 2` gives `3`, not `3.5`. If you need decimal division, at least one value should be `double`.
The `%` operator gives the remainder after division. It is very useful in DSA because it helps check even/odd numbers, divisibility, cyclic movement, last digit extraction, and modular arithmetic.
Relational Operators
Relational operators compare two values and return a boolean result: either `true` or `false`. These operators are commonly used inside `if`, `else if`, loops, and search conditions.
- `>` greater than
- `<` less than
- `>=` greater than or equal to
- `<=` less than or equal to
- `==` equal to
- `!=` not equal to
For example, `marks >= 40` checks whether marks are at least 40. The result of this expression is not a number; it is a boolean value. If the condition is true, the program can take one path. If it is false, the program can take another path.
Logical Operators
Logical operators are used when one condition is not enough. They allow us to combine multiple conditions and make better decisions.
- `&&` returns true when both conditions are true
- `||` returns true when at least one condition is true
- `!` reverses the condition
Use `&&` when all conditions must be true. For example, a student may pass only when marks are at least 40 and attendance is also enough. Use `||` when any one condition is enough. Use `!` when you want the opposite of a condition.
Assignment Operators
Assignment operators are used to store or update values in variables. The most basic assignment operator is `=`, which puts the right-side value into the left-side variable. `+=`, `-=`, `*=`, `/=`, and `%=` are shorthand assignment operators. They make update operations shorter and cleaner.
```java
int sum = 10;
sum += 5; // same as sum = sum + 5
```
In loops and DSA problems, assignment operators are used frequently to update counters, sums, products, indexes, and answers.
Increment and Decrement
`++` increases a value by 1, and `--` decreases a value by 1. These operators are commonly used in loops because loop variables usually move step by step.
```java
int i = 1;
i++;
```
After this code, the value of `i` becomes `2`. In beginner DSA, you will mostly use `i++` in loops such as `for (int i = 0; i < n; i++)`.
Ternary Operator
The ternary operator is a short form of a simple `if else`. It is useful when you want to choose one value from two options based on a condition.
```java
int greater = (a > b) ? a : b;
```
If `a > b` is true, `greater` gets `a`. Otherwise, it gets `b`. Ternary is good for simple value selection, but for long logic, normal `if else` is easier to read.
Operator Precedence
Operator precedence decides which operation runs first when an expression has multiple operators. For example, multiplication runs before addition.
```java
int result = 10 + 5 * 2;
```
Here, `5 * 2` runs first, so the result is `20`, not `30`. To make the expression clearer, you can use parentheses.
```java
int result = (10 + 5) * 2;
```
Now the addition runs first, so the result is `30`. In coding problems, parentheses make expressions easier to understand and help avoid mistakes.
Example Program
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.print("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt(); System.out.println("Sum = " + (a + b));
System.out.println("Difference = " + (a - b));
System.out.println("Product = " + (a * b)); if (b != 0) {
System.out.println("Quotient = " + (a / b));
System.out.println("Remainder = " + (a % b));
} else {
System.out.println("Division by zero is not allowed");
}
int greater = (a > b) ? a : b;
System.out.println("Greater = " + greater);
}
}
``` This program uses arithmetic operators for calculation, a relational operator to check `b != 0`, and the ternary operator to find the greater number. The division check is important because division or remainder by zero is not allowed.
Operators in DSA
Operators are used in almost every DSA problem. Arithmetic operators calculate sums and indexes. Relational operators compare values. Logical operators combine conditions. Assignment and increment operators update loop variables and answers.
When you solve array, string, loop, searching, or sorting problems, operators become part of the logic. So instead of memorizing operators only as symbols, understand where they are used in real problems.
Practice Question
Write a Java program that takes two numbers as input and prints their sum, difference, product, quotient, remainder, and the greater number. Also handle the case when the second number is zero.