Operators in Java
Operators are the symbols that let you compute with values — add numbers, compare them, combine boolean conditions, and assign results. Java groups them into arithmetic (+ - * / %), relational (< > == !=), logical (&& || !), assignment (= += ...), bitwise (& | ^ << >>), and the conditional "ternary" operator (? :). Knowing what each does — and the order they run in — keeps your expressions correct.
Arithmetic operators
int a = 17, b = 5;
System.out.println(a + b); // 22 addition
System.out.println(a - b); // 12 subtraction
System.out.println(a * b); // 85 multiplication
System.out.println(a / b); // 3 integer division (drops the fraction)
System.out.println(a % b); // 2 modulo: the remainder of 17 / 5
Integer division truncates toward zero. To keep the decimal, make at least one operand a double: 17.0 / 5 is 3.4. The % (modulo) operator is handy for checking divisibility — n % 2 == 0 means n is even.
Relational operators
These compare two values and produce a boolean:
int score = 72;
System.out.println(score > 60); // true
System.out.println(score == 72); // true (== tests equality)
System.out.println(score != 80); // true (!= tests inequality)
Use == to compare primitives. For objects like String, == checks whether two references point at the same object — to compare contents use .equals() (covered in the strings tutorial).
Logical operators
boolean hasFees = true;
boolean hasSeat = false;
System.out.println(hasFees && hasSeat); // false AND: both must be true
System.out.println(hasFees || hasSeat); // true OR: at least one true
System.out.println(!hasSeat); // true NOT: flips the value
&& and || short-circuit: if the left side already decides the result, the right side is never evaluated. This is both an efficiency and a safety feature — x != null && x.isReady() will not call a method on null.
Assignment operators
int total = 100;
total += 25; // same as: total = total + 25; -> 125
total -= 10; // 115
total *= 2; // 230
total /= 5; // 46
total %= 7; // 4
Compound assignment operators read and update a variable in one step.
Increment and decrement
int i = 5;
System.out.println(i++); // prints 5, THEN i becomes 6 (post-increment)
System.out.println(++i); // i becomes 7 FIRST, then prints 7 (pre-increment)
The difference between i++ and ++i only matters when you use the value in the same expression.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesBitwise operators
These work on the individual bits of integer types. You meet them less often as a beginner, but they are useful for flags and low-level work.
int x = 0b1100; // 12
int y = 0b1010; // 10
System.out.println(x & y); // 8 bitwise AND -> 0b1000
System.out.println(x | y); // 14 bitwise OR -> 0b1110
System.out.println(x ^ y); // 6 bitwise XOR -> 0b0110
System.out.println(x << 1); // 24 shift left (multiply by 2)
The ternary (conditional) operator
A compact if/else that returns a value:
int marks = 45;
String result = (marks >= 40) ? "Pass" : "Fail"; // condition ? ifTrue : ifFalse
System.out.println(result); // Pass
Operator precedence
When several operators appear together, Java applies them in a fixed order: multiplicative (* / %) before additive (+ -), relational before logical, and so on. When in doubt, add parentheses — they make intent explicit and never hurt.
int value = 2 + 3 * 4; // 14, because * runs before +
int clear = (2 + 3) * 4; // 20, parentheses force addition first
Common mistakes
- Using
==to compare Strings. That compares references, not text. Use.equals(). - Integer division losing the fraction.
1 / 2is0. Cast or use adoubleliteral. - Confusing
=and==.=assigns;==compares. Java rejectsif (x = 5)unlessxis a boolean, which catches many such typos. - Relying on
&instead of&&for conditions.&does not short-circuit and always evaluates both sides — risky if the right side could fail. - Over-trusting precedence. Mixed bitwise and arithmetic in one line is hard to read; parenthesize.
FAQ
What does % do with negatives? The result takes the sign of the left operand: -7 % 3 is -1.
Is the ternary operator worth using? Yes, for short value choices. For multi-branch logic, a full if/else or switch reads better.
Keep going
Next, control the flow of your program with Control Flow in Java, or review Variables & Data Types. See all tutorials on the Java hub; for guided practice in Jalgaon, see the Java course.
Learn Java the structured way at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
