Java Numbers
2 min read ·
Numbers in Java are divided into two main categories:
- Integer types
- Floating point types
Integer Types in Java
Integer types store whole numbers.
- byte
- short
- int
- long
Example:
Floating Point Types
Floating point types store decimal numbers.
- float
- double
Example:
Difference Between float and double
| Feature | float | double |
|---|---|---|
| Size | 4 bytes | 8 bytes |
| Precision | Lower | Higher |
| Suffix Required | f | Not required |
Example showing precision difference:
double gives more accurate result.Scientific Notation in Java
Java allows writing numbers in scientific notation using
e or E.Example:
Output:
- 1.5e2 means 1.5 × 10²
- 2E3 means 2 × 10³
Numeric Literals
Java supports different numeric literal formats.
Decimal:
Binary:
Hexadecimal:
Complete example:
Understanding data types is essential because they control:
- Memory usage
- Performance
- Type safety
- Program correctness
These concepts form the foundation of all Java programming.