Java has six primitive numeric types. They differ in size and range:

TypeSizeRangeDefault
byte8-bit-128 to 1270
short16-bit-32,768 to 32,7670
int32-bit-2,147,483,648 to 2,147,483,6470
long64-bit-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
float32-bit IEEE 754~±3.4e38 (7 decimal digits)0.0f
double64-bit IEEE 754~±1.8e308 (15 decimal digits)0.0d
int age = 30; long population = 8_000_000_000L; // underscore separator for readability float price = 9.99f; double pi = 3.141592653589793; // Integer.MAX_VALUE and MIN_VALUE constants System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println(Long.MIN_VALUE); // -9223372036854775808

Each primitive has a corresponding wrapper class in java.lang. They are used with generics (e.g. List<Integer>) and provide useful utility methods.

// Autoboxing: primitive -> wrapper Integer x = 42; // Unboxing: wrapper -> primitive int y = x; // Useful static methods int parsed = Integer.parseInt("123"); String s = Integer.toString(255, 16); // "ff" (hex) int max = Integer.max(10, 20); // 20 // Comparing wrapper objects — use .equals() not == Integer a = 200, b = 200; System.out.println(a == b); // false (outside cache range -128..127) System.out.println(a.equals(b)); // true

BigDecimal should be used for financial and monetary calculations where floating-point precision errors are unacceptable.

// Never use double constructor — it preserves the floating-point imprecision BigDecimal bad = new BigDecimal(0.1); // 0.1000000000000000055511151231257827021181583404541015625 // Use String constructor or valueOf BigDecimal price = new BigDecimal("19.99"); BigDecimal tax = new BigDecimal("0.08"); BigDecimal total = price.multiply(tax); total = total.setScale(2, RoundingMode.HALF_UP); // 1.60 // Arithmetic BigDecimal a = new BigDecimal("10.5"); BigDecimal b = new BigDecimal("3.2"); System.out.println(a.add(b)); // 13.7 System.out.println(a.subtract(b)); // 7.3 System.out.println(a.divide(b, 4, RoundingMode.HALF_UP)); // 3.2813 // Comparison — use compareTo(), not equals() (scale matters for equals) BigDecimal x = new BigDecimal("2.0"); BigDecimal y = new BigDecimal("2.00"); System.out.println(x.equals(y)); // false (different scale) System.out.println(x.compareTo(y)); // 0 (same value)

Java integer arithmetic wraps around silently on overflow. Use Math.addExact() and related methods to get an ArithmeticException instead.

int max = Integer.MAX_VALUE; System.out.println(max + 1); // -2147483648 (overflow, no exception) // Safe arithmetic — throws ArithmeticException on overflow int result = Math.addExact(max, 1); // throws ArithmeticException long safe = Math.multiplyExact(100000L, 100000L); // 10000000000