Java Numbers - Complete Guide
Introduction to Numeric Data Types
Java provides multiple numeric data types to represent whole numbers and decimals, each optimized for range and precision.
Integer types (byte, short, int, long) store whole numbers, while floating-point types (float, double) store decimal numbers. Choosing the right type prevents overflow and improves performance.
Integer Data Types
Java supports four integer types, differing in size, range, and common use cases:
Type | Size | Range | Use Case |
---|---|---|---|
byte | 8 bits | -128 to 127 | Small numbers, file I/O, memory conservation |
short | 16 bits | -32,768 to 32,767 | Medium-sized numbers, legacy use |
int | 32 bits | -2^31 to 2^31-1 | General-purpose, most commonly used |
long | 64 bits | -2^63 to 2^63-1 | Very large numbers, timestamps, big calculations |
Floating-Point Data Types
Java provides two floating-point types for decimal numbers, differing in precision:
public class FloatingPointTypes {
public static void main(String[] args) {
float floatValue = 3.1415927f; // 32 bits
double doubleValue = 3.141592653589793; // 64 bits
System.out.println("Float: " + floatValue);
System.out.println("Double: " + doubleValue);
float scientificFloat = 1.234e-3f;
double scientificDouble = 2.998e8;
System.out.println("Scientific Float: " + scientificFloat);
System.out.println("Scientific Double: " + scientificDouble);
System.out.println("Infinity: " + Double.POSITIVE_INFINITY);
System.out.println("NaN: " + Double.NaN);
float f1 = 1.0f / 3.0f;
double d1 = 1.0 / 3.0;
System.out.println("Float precision: " + f1);
System.out.println("Double precision: " + d1);
}
}
Float: 3.1415927 Double: 3.141592653589793 Scientific Float: 0.001234 Scientific Double: 2.998E8 Infinity: Infinity NaN: NaN Float precision: 0.33333334 Double precision: 0.3333333333333333
Numeric Operations and Precision
Java supports arithmetic operations on numbers. It’s important to understand integer vs floating-point division, overflow, and precision issues.
public class NumericOperations {
public static void main(String[] args) {
int a = 5, b = 2;
System.out.println("Integer division: " + (a / b));
System.out.println("Floating division: " + (a / (double)b));
System.out.println("Modulus: " + (a % b));
byte maxByte = Byte.MAX_VALUE;
System.out.println("Max byte: " + maxByte);
System.out.println("Overflow: " + (byte)(maxByte + 1));
double result = 0.1 + 0.2;
System.out.println("0.1 + 0.2 = " + result);
System.out.println("Square root: " + Math.sqrt(25));
System.out.println("Power: " + Math.pow(2, 3));
System.out.println("Round: " + Math.round(3.7));
System.out.println("Absolute: " + Math.abs(-5));
}
}
Integer division: 2 Floating division: 2.5 Modulus: 1 Max byte: 127 Overflow: -128 0.1 + 0.2 = 0.30000000000000004 Square root: 5.0 Power: 8.0 Round: 4 Absolute: 5
Number Formatting and Conversion
Java offers flexible ways to format and convert numbers using NumberFormat, Currency formatting, and parsing from strings.
import java.text.NumberFormat;
import java.util.Locale;
public class NumberFormatting {
public static void main(String[] args) {
double amount = 1234567.89;
NumberFormat usFormat = NumberFormat.getInstance(Locale.US);
NumberFormat frFormat = NumberFormat.getInstance(Locale.FRANCE);
NumberFormat deFormat = NumberFormat.getInstance(Locale.GERMANY);
System.out.println("US format: " + usFormat.format(amount));
System.out.println("French format: " + frFormat.format(amount));
System.out.println("German format: " + deFormat.format(amount));
NumberFormat usCurrency = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat jpCurrency = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println("US currency: " + usCurrency.format(amount));
System.out.println("Japanese currency: " + jpCurrency.format(amount));
NumberFormat percentFormat = NumberFormat.getPercentInstance();
System.out.println("Percentage: " + percentFormat.format(0.85));
try {
String numberStr = "1,234.56";
double parsedNumber = usFormat.parse(numberStr).doubleValue();
System.out.println("Parsed number: " + parsedNumber);
} catch (Exception e) {
System.out.println("Parse error: " + e.getMessage());
}
}
}
US format: 1,234,567.89 French format: 1 234 567,89 German format: 1.234.567,89 US currency: $1,234,567.89 Japanese currency: ¥1,234,568 Percentage: 85% Parsed number: 1234.56