Limited Offer
60% OFF on all plans!

Numbers in Java

Work with integer and floating-point types, understand overflow, use the Math class, and handle precise decimals with BigDecimal.

Creating Numbers

Java has multiple numeric types. `int` covers most whole-number needs, `long` handles very large integers, `double` is the default for decimals, and `float` saves memory at the cost of precision. Use numeric literal suffixes (`L`, `f`, `d`) to be explicit.

1. Integer Numbers

Use `int` for everyday whole numbers and `long` when the value exceeds ~2.1 billion.

2. Decimal Numbers

Use `double` for most decimal work. Use `float` only when memory is tightly constrained.

Finding Min and Max

Java's `Math` class provides `Math.min()` and `Math.max()` for comparing two values. For finding the min or max in an array or collection, iterate with a loop or use `Arrays`/streams.

1. Finding Minimum

Compare two values with `Math.min()`, or scan an array to find the overall minimum.

2. Finding Maximum

Compare two values with `Math.max()`, or scan an array to find the overall maximum.

Rounding Numbers

`Math.round()` rounds to the nearest integer. For rounding to N decimal places use `Math.round()` with scaling, `String.format()`, or `BigDecimal` for exact control.

1. Rounding to the Nearest Integer

`Math.round()` follows standard rounding: .5 and above rounds up, below .5 rounds down.

2. Rounding to N Decimal Places

Scale the number up, round it, then scale back down — or use String.format() for display.

Random Numbers

`Math.random()` returns a `double` between 0.0 (inclusive) and 1.0 (exclusive). The `java.util.Random` class gives more control — specific ranges, different types, and a seedable generator. Java 17+ also has `RandomGenerator` interface.

1. Random Number from 0 to 1

`Math.random()` always returns a value in [0.0, 1.0). Use it as the base for any range.

2. Random Integer from a to b

Generate a random integer in a specific range [a, b] inclusive using `Math.random()` or `Random`.

Powers and Roots

The `Math` class provides `Math.pow()` for exponentiation and `Math.sqrt()` / `Math.cbrt()` for roots. For integer powers consider bit-shifting for powers of 2.

1. Powers (a^b)

Use `Math.pow(base, exponent)` to raise any number to any power.

2. Square Root, Cube Root & Nth Root

`Math.sqrt()` and `Math.cbrt()` are dedicated methods. Any nth root is `Math.pow(x, 1.0/n)`.

Number ↔ String Conversion

Conversion between numbers and strings is a very common task — reading user input, formatting output, processing CSV data. Java provides static methods on wrapper classes for both directions.

1. Number → String

Convert any numeric type to its String representation.

2. String → Number

Parse a numeric string to its primitive type. Always handle `NumberFormatException` for invalid input.

Validating Numbers

Java does not have a built-in `isNumeric()` method on `String`. The standard patterns are: `try-catch` around `parseInt`, a helper method, or a regex check.

1. Checking if a String is a Valid Number

Three approaches: try-catch, regex, and a reusable helper method.

Overflow & BigDecimal

Integer overflow silently wraps around in Java — no exception is thrown. For financial calculations where rounding errors are unacceptable, use `BigDecimal` instead of `double`.

1. Integer Overflow

When an integer exceeds its max value it wraps around silently. Detect it with `Math.addExact()` or use `long`.

2. BigDecimal (Precise Decimal Arithmetic)

Use `BigDecimal` for financial and scientific calculations where floating-point precision errors are not acceptable.