Beginner · Getting started with Java · topic 4 of 60 · 25 min
Operators and expressions
Explanation
Arithmetic
System.out.println(7 + 2); // 9
System.out.println(7 - 2); // 5
System.out.println(7 * 2); // 14
System.out.println(7 / 2); // 3 (int / int → int, the decimal part is cut off)
System.out.println(7 % 2); // 1 (remainder)
System.out.println(7.0 / 2); // 3.5 (one double makes it a double division)
System.out.println(-7 / 2); // -3 (truncates toward zero)
System.out.println(-7 % 2); // -1 (the sign follows the left side)Assignment and compound operators
int stock = 50;
stock += 10; // 60
stock -= 5; // 55
stock *= 2; // 110
stock /= 3; // 36 (int division)
stock %= 10; // 6
byte b = 10;
// b = b + 1; // compile error: b + 1 is an int, cannot store in byte without a cast
b += 1; // OK: compound operators include an automatic castIncrement and decrement
int i = 5;
int a = i++; // a = 5, then i becomes 6 (postfix: use, then add)
int b = ++i; // i becomes 7, then b = 7 (prefix: add, then use)
System.out.println(a + " " + b + " " + i); // 5 7 7Comparison and logical operators
Comparisons (==, !=, <, <=, >, >=) give a boolean. && (and) and || (or) combine booleans and short-circuit: in a && b, if a is false, b is never evaluated. This is used for safety checks like name != null && name.length() > 0.
int age = 20;
boolean hasId = true;
System.out.println(age >= 18 && hasId); // true
System.out.println(age < 13 || age > 60); // false
System.out.println(!hasId); // falseOverflow
An int has a fixed size, so going past its maximum wraps around to the most negative value without any error. This is called overflow. Use long for large values, or Math.addExact / Math.multiplyExact, which throw an ArithmeticException on overflow instead of silently wrapping.
int max = Integer.MAX_VALUE; // 2147483647
System.out.println(max + 1); // -2147483648 (overflow!)
long ok = (long) max + 1; // 2147483648
// Math.addExact(max, 1); // throws ArithmeticException: integer overflowPrecedence and Math
As in school: * / % before + -, comparisons after arithmetic, then &&, then ||. Use brackets when in doubt. The Math class gives Math.max, Math.min, Math.abs, Math.pow (returns double), Math.sqrt, Math.round (returns long for a double argument), Math.floor and Math.ceil.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Operators and expressions · 1/6
In this lesson you will calculate and compare with Java operators.
Prefer a video by a teacher? Find free videos on “java operators integer division increment overflow tutorial” on YouTube ↗
In real life: Splitting a bill and checking eligibility
A group-ordering app splits a ₹1,000 bill between 3 people and shows the remainder, then checks whether the order qualifies for free delivery. Note the integer division and % for the leftover rupee.
public class Split {
public static void main(String[] args) {
int bill = 1000, people = 3;
int each = bill / people; // 333
int left = bill % people; // 1
boolean freeDelivery = bill >= 499 && people <= 4;
System.out.println(each + " each, " + left + " left, free delivery: " + freeDelivery);
}
}Practice (0/3 done)
What does this print? int x = 10; int y = x++ + ++x; System.out.println(x + " " + y);
Which line does NOT compile? 1: short s = 5; 2: s += 3; 3: s = s + 3; 4: int t = s + 3;
The same integer-division idea in JavaScript, which runs here: JavaScript has no int type, so write javaIntDivide(a, b) that returns what Java's int division a / b would give (truncate toward zero), and javaRemainder(a, b) for Java's % (same sign as a).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Type conversion and reading inputGetting started with Java · same module
- Installing the JDK and your first programGetting started with Java · same module
- OperatorsC Programming (from zero to systems) · Operators and control flow
- Operators and type coercionJavaScript Programming (language in depth) · First steps: values, variables and operators
- Operators and decisions: if, else and switchC++ Programming (modern C++ and DSA) · Decisions, loops and functions
- Operators, conversions and parsing inputC# and .NET · Getting started with C# and .NET
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is 17 / 5 in Java with ints?
Answer: 3
Integer division drops the decimal part.
What is 17 % 5?
Answer: 2
5 × 3 = 15, remainder 2.
In a && b, when is b NOT evaluated?
Answer: When a is false
&& short-circuits: if a is false, the result is false regardless of b.
Integer.MAX_VALUE + 1 gives…
Answer: Integer.MIN_VALUE (-2147483648)
int overflow wraps around silently.
Which expression is undefined behaviour?Related
int a = 7; int b = (a % 2 == 0) ? 1 : 0; What is b?Related
Answer: 0
7 % 2 is 1, so the condition is false and b gets 0.
What does Math.Round(2.5) return in C#?Related
Answer: 2
By default C# uses banker's rounding: halfway values go to the nearest even number.
Is it legal to compute a + n for int a[n]?Related
Answer: Yes, one past the end may be computed but not dereferenced
The one-past-the-end rule.
From Pointer arithmetic
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Getting started with Java” before its exam: Type conversion and reading input.
- Module 1 exam: Getting started with Java · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Control flow and methods. 60 topics left in the course before the final exam.