Learn Java
import com.learnjava.variablesdatatypes.Numbers;

Numbers in Java

@Concept

Working with numbers in Java means choosing among several numeric primitives and knowing how the standard library rounds out what the primitives alone can't do. The Math class supplies static methods for anything beyond basic arithmetic — Math.pow, Math.sqrt, Math.abs, Math.max, Math.round, and dozens more — all callable without creating an object.

A quieter but important detail: dividing two integers always produces an integer result, discarding any remainder, even when the mathematical answer clearly has a fractional part. Getting a decimal result out of integer inputs means making at least one operand a double first, typically by casting or multiplying by 1.0.

@HowItWorks
1

Integer division truncates

7 / 2 evaluates to 3, not 3.5 — dividing two ints always produces an int, discarding the remainder entirely.

2

Getting a decimal result

Making at least one operand a double, such as by casting one side, forces the division itself to produce a fractional result instead of a truncated one.

3

The modulo operator

The percent sign returns the remainder of a division: 7 % 2 is 1, which is genuinely useful on its own, not just a side effect of division.

4

The Math class

Static methods like Math.pow, Math.sqrt, and Math.abs cover everything ordinary arithmetic operators don't, with no object to construct first.

5

Rounding

Math.round rounds to the nearest whole number, while a narrowing cast on a decimal always truncates toward zero instead — the two are not interchangeable.

@Example
public class NumberDemo {
public static void main(String[] args) {
int a = 7, b = 2;
System.out.println("Integer division: " + (a / b));
System.out.println("Decimal division: " + (a / (double) b));
System.out.println("Remainder: " + (a % b));
System.out.println("Square root of 49: " + Math.sqrt(49));
}
}
Output
Integer division: 3
Decimal division: 3.5
Remainder: 1
Square root of 49: 7.0
1a / b divides two ints, so the result truncates to 3, discarding the 0.5.
2a / (double) b casts b to a double first, so the division itself produces the precise 3.5.
3a % b is the remainder of 7 divided by 2, which is 1.
4Math.sqrt(49) calls a static method on Math with no object to create, and always returns a double, hence 7.0 rather than 7.
@UseCases
Debugging a calculation that unexpectedly returns 0 or a truncated whole number instead of a decimal
Computing distances, areas, or other formulas that need Math.sqrt, Math.pow, or similar
Using the remainder operator to check even or odd, wrap an index around an array's length, or convert units
Choosing between Math.round and a narrowing cast depending on whether the goal is rounding or truncation

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.