Numbers
Java provides primitive numeric data types (int, long, float, double) as well as the wrapper classes Integer, Long, Float, and Double, which provide utility methods. The Math class from java.lang provides standard math functions such as Math.abs(), Math.max(), Math.min(), Math.sqrt(), and Math.pow().
Java 8+ added java.util.Random for random number generation and ThreadLocalRandom for better performance in multithreading. For large, precise numbers, use BigInteger (large integers) and BigDecimal (large decimals) from the java.math package.
Conversion between numeric types can be done via widening (automatic, small to large) or narrowing (manual/explicit cast, large to small). Narrowing casts risk losing data, so an explicit cast with (type) is required.
Numeric Types:
| Category | Primitive | Wrapper | Use |
|---|---|---|---|
| Integer | byte, short, int, long |
Byte, Short, Integer, Long |
Whole numbers |
| Decimal | float, double |
Float, Double |
Decimal numbers |
| High precision | - | BigInteger, BigDecimal |
Very large/exact numbers |
Code Example
public class JavaNumbers {
public static void main(String[] args) {
// Numeric types
int a = 100;
double b = 3.14159;
long c = 9999999999L;
// Math class
System.out.println("abs(-10) = " + Math.abs(-10)); // 10
System.out.println("max(10,20) = " + Math.max(10, 20)); // 20
System.out.println("sqrt(144) = " + Math.sqrt(144)); // 12.0
System.out.println("pow(2,10) = " + Math.pow(2, 10)); // 1024.0
// Random
int random = (int) (Math.random() * 100) + 1; // 1-100
System.out.println("Random number: " + random);
// Type casting
double pi = 3.14;
int piInt = (int) pi; // narrowing cast, result: 3
System.out.println("Pi as int: " + piInt);
}
}
Code Explanation
Math.abs() returns the absolute (positive) value. Math.max(a, b) returns the larger value. Math.sqrt() computes the square root. Math.pow(a, b) computes a to the power of b; the result is a double. Math.random() returns a random number between 0.0 (inclusive) and 1.0 (exclusive) — multiplied by 100 and cast to int to get a whole number from 0-99, then adding 1 makes it 1-100. The narrowing cast (int) pi truncates the decimal part without rounding.
Everyday Example
A simple statistics calculator program to compute the average, minimum, and maximum from sales data:
public class SalesStatistics {
public static void main(String[] args) {
int[] sales = {1500000, 2300000, 1800000, 3100000, 2750000};
int total = 0, min = sales[0], max = sales[0];
for (int value : sales) {
total += value;
min = Math.min(min, value);
max = Math.max(max, value);
}
double average = (double) total / sales.length;
System.out.println("Total: $" + total);
System.out.println("Average: $" + String.format("%,.0f", average));
System.out.println("Minimum: $" + min);
System.out.println("Maximum: $" + max);
}
}
Summary
Java provides a complete numeric toolset, from primitive types to BigInteger/BigDecimal. The Math class is an essential utility that you should master for everyday math operations. Always be careful about precision loss when converting between types, especially from double to int.