Operators in Java
In this lesson: Arithmetic, relational, logical, bitwise, assignment, unary and ternary operators in Java — with output. In Hindi & English.
What are operators
Operators are symbols that do things with values — add them, compare them, or combine conditions. They are the basic building blocks of every calculation and decision in a program. Java groups them into arithmetic, assignment, comparison, logical, and increment/decrement.
+ adds, == compares, && combines conditions.Arithmetic operators
int a = 10, b = 3; System.out.println(a + b); // 13 System.out.println(a - b); // 7 System.out.println(a * b); // 30 System.out.println(a / b); // 3 (integer division) System.out.println(a % b); // 1 (remainder)
7
30
3
1
Note a / b with two ints gives 3, not 3.33 — integer division drops the decimal. % gives the remainder, great for even/odd checks.
Assignment operators
int x = 10; x += 5; // x = x + 5 -> 15 x -= 3; // x = x - 3 -> 12 x *= 2; // x = x * 2 -> 24 System.out.println(x);
= assigns a value. Shortcuts like += and *= update a variable using its own current value, saving typing.
Comparison operators
int a = 10, b = 3; System.out.println(a == b); // false System.out.println(a != b); // true System.out.println(a > b); // true System.out.println(a <= b); // false
true
true
false
These compare two values and give a boolean (true/false). Remember == compares, while a single = assigns.
Logical operators
int age = 20; boolean ok = (age > 18) && (age < 60); // AND boolean any = (age < 12) || (age > 18); // OR System.out.println(ok); System.out.println(!ok); // NOT
false
&& (AND) is true only if both sides are true. || (OR) is true if either side is. ! (NOT) flips true and false.
Increment and decrement
int i = 5; System.out.println(i++); // prints 5, then i becomes 6 System.out.println(++i); // i becomes 7, then prints 7
7
i++ uses the value first, then adds 1. ++i adds 1 first, then uses it. Both are common in loops as a short way to count.
Common mistakes
- Using
=instead of==.=assigns;==compares. - Integer division surprise.
5 / 2is 2, not 2.5 — use a double for decimals. - Confusing
i++and++i. The order of "use" and "add" differs. - Using
&for conditions. Use&&and||for logic, not the bitwise&.
Summary
- Operators act on values for calculations and decisions.
- Arithmetic:
+ - * / %(watch integer division). - Comparison returns true/false;
==compares,=assigns. - Logical
&&,||,!combine conditions. ++and--increase or decrease by 1, with pre/post forms.
Next, make decisions with if-else in Java, or repeat actions with Java loops.
Frequently Asked Questions
What are operators in Java?
+ to add, > to compare, and && to combine conditions. They are the basic tools for calculations and decisions in a program.What does the % (modulus) operator do?
% gives the remainder after division. For example 10 % 3 is 1. It is commonly used to check even/odd numbers (n % 2 == 0) or to cycle through values.What is the difference between = and ==?
= assigns a value to a variable. A double == compares two values and returns true or false. Mixing them up is a very common beginner error.What is the difference between ++i and i++?
++i (pre-increment) increases first, then uses the value; i++ (post-increment) uses the current value first, then increases. The difference matters when used inside a bigger expression.What is the difference between && and &?
&& is the logical AND for conditions and short-circuits (stops early if the first is false). A single & is a bitwise operator. For if-conditions, always use && and ||.