In this tutorial
Operators are the verbs of a programming language. They add numbers, compare values, combine conditions and update variables. You will use them in almost every line of Java you write, so it pays to understand exactly how they behave, including the edge cases that surprise beginners.
This tutorial covers every operator group you need day to day, with runnable examples.
Arithmetic operators
+addition (also joins strings)-subtraction*multiplication/division%remainder (modulo)
public class Arithmetic {
public static void main(String[] args) {
int a = 17;
int b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3 (integer division drops the remainder)
System.out.println(a % b); // 2
System.out.println(17.0 / 5); // 3.4
}
}
Integer division and remainder
When both sides of / are integers, the result is an integer and the fractional part is thrown away (truncated toward zero). To get a decimal result, make at least one operand a double.
The % operator is more useful than it looks:
n % 2 == 0tells you whethernis even.index % sizewraps an index around, which is handy for circular buffers.seconds % 60gives the seconds left over after whole minutes.
Note that in Java the sign of % follows the left operand: -7 % 3 is -1, not 2. If you need a non-negative result, use Math.floorMod(-7, 3), which returns 2.
Assignment operators
= stores a value. The compound operators combine an operation with assignment:
int balance = 1000;
balance += 250; // balance = balance + 250 -> 1250
balance -= 100; // 1150
balance *= 2; // 2300
balance /= 4; // 575
balance %= 100; // 75
Compound operators also perform an implicit cast. byte b = 10; b += 5; compiles, but b = b + 5; does not, because b + 5 is an int.
Increment and decrement
++ adds one and -- subtracts one. Where you put them matters when the expression is used inside another expression:
int x = 5;
int y = x++; // y = 5, then x becomes 6 (post-increment)
int z = ++x; // x becomes 7, then z = 7 (pre-increment)
The safest habit is to use ++ and -- as standalone statements (count++;) so nobody has to think about the order.
Comparison operators
These return a boolean:
==equal to!=not equal to>greater than,<less than>=greater than or equal,<=less than or equal
int age = 18;
System.out.println(age >= 18); // true
System.out.println(age == 21); // false
== versus equals()
For primitives, == compares values. For objects, == checks whether two references point to the same object, not whether the objects contain the same data.
String a = new String("java");
String b = new String("java");
System.out.println(a == b); // false: two different objects
System.out.println(a.equals(b)); // true: same text
Always compare strings, and most other objects, with equals().
Logical operators
Use these to combine boolean conditions:
&&AND: true only if both sides are true||OR: true if at least one side is true!NOT: flips true to false and back
boolean hasTicket = true;
boolean isVip = false;
int age = 20;
if (hasTicket && age >= 18) {
System.out.println("Welcome in");
}
if (isVip || hasTicket) {
System.out.println("Proceed to the gate");
}
if (!isVip) {
System.out.println("Standard seating");
}
Short-circuit evaluation
&& stops as soon as the left side is false, and || stops as soon as the left side is true. This lets you write safe checks like:
String name = null;
if (name != null && name.length() > 3) {
System.out.println("Long name");
}
If name is null, the second part never runs, so there is no NullPointerException. The single-character versions & and | evaluate both sides every time, which is rarely what you want for conditions.
The ternary operator
condition ? valueIfTrue : valueIfFalse is a compact if-else that produces a value:
int score = 72;
String result = score >= 50 ? "Pass" : "Fail";
System.out.println(result); // Pass
Use it for short, simple choices. If you find yourself nesting ternaries, switch to a regular if statement for readability.
String concatenation with +
When either operand of + is a String, Java joins them:
System.out.println("Total: " + 10 + 5); // Total: 105
System.out.println("Total: " + (10 + 5)); // Total: 15
Expressions are evaluated left to right, so in the first line "Total: " + 10 becomes a string first. Brackets fix it.
Bitwise and shift operators
You will meet these less often, mainly in low-level code, flags and interview puzzles:
&,|,^bitwise AND, OR, XOR on integers~bitwise NOT<<left shift,>>signed right shift,>>>unsigned right shift
int flags = 0b0101;
System.out.println(flags & 0b0100); // 4: the third bit is set
System.out.println(1 << 3); // 8: shifting left multiplies by 2 each step
Operator precedence
When an expression mixes operators, Java follows precedence rules, roughly:
- Postfix
x++ x-- - Unary
++x --x ! - - Multiplicative
* / % - Additive
+ - - Relational
< > <= >= - Equality
== != - Logical AND
&& - Logical OR
|| - Ternary
? : - Assignment
= += -=and so on
So 2 + 3 * 4 is 14, not 20. You do not need to memorise the full table. When in doubt, add brackets: they cost nothing and make your intent obvious to the next reader.
Putting it together
This small program decides whether a year is a leap year using %, &&, || and !:
public class LeapYear {
static boolean isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public static void main(String[] args) {
int[] years = {1900, 2000, 2024, 2025};
for (int year : years) {
System.out.println(year + " is leap? " + isLeap(year));
}
}
}
Output:
1900 is leap? false
2000 is leap? true
2024 is leap? true
2025 is leap? false
Summary
- Arithmetic operators behave differently for integers and decimals; watch integer division.
%is great for even/odd checks and wrapping values.- Use
equals()for objects and==for primitives. &&and||short-circuit, which you can use to guard againstnull.- When precedence is unclear, add brackets.
Next up: making decisions with if, else and switch in Java.