Java Numbers
Number data types in Java are used to store numeric values. They are divided into two categories: integer types and floating-point types.
Key Topics
1. Integer Types
Integer types store whole numbers and include:
Type | Size | Range |
---|---|---|
byte | 8 bits | -128 to 127 |
short | 16 bits | -32,768 to 32,767 |
int | 32 bits | -231 to 231-1 |
long | 64 bits | -263 to 263-1 |
2. Floating-Point Types
Floating-point types store numbers with fractional parts and include:
Type | Size | Range |
---|---|---|
float | 32 bits | Approximately ±3.40282347E+38F |
double | 64 bits | Approximately ±1.79769313486231570E+308 |
3. Examples
public class NumberExamples {
public static void main(String[] args) {
int intNum = 100;
double doubleNum = 123.45;
System.out.println("Integer Number: " + intNum);
System.out.println("Double Number: " + doubleNum);
}
}
Output:
Integer Number: 100
Double Number: 123.45
Double Number: 123.45
Key Takeaways
- Choose the appropriate numeric type based on the required range and precision.
- Use integer types for whole numbers and floating-point types for numbers with decimal points.