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
byte8 bits-128 to 127
short16 bits-32,768 to 32,767
int32 bits-231 to 231-1
long64 bits-263 to 263-1

2. Floating-Point Types

Floating-point types store numbers with fractional parts and include:

Type Size Range
float32 bitsApproximately ±3.40282347E+38F
double64 bitsApproximately ±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

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.