← Back to Home

Size & Range of Data Types

Understanding the size and range of data types is critical for memory efficiency, performance, and avoiding overflow/underflow bugs. Java defines fixed sizes for primitive data types to ensure platform independence.

Why Size & Range Matter

  • Prevents data overflow and truncation
  • Helps choose the right data type
  • Ensures consistent behavior across platforms
  • Important for interviews and real-time applications

Primitive Data Types: Size & Range

Java has 8 primitive data types, each with a fixed size.

1. Integer Data Types

Used to store whole numbers.

byte

  • Size: 1 byte (8 bits)
  • Range: -128 to 127
byte b = 100;
          

When to use: Low memory scenarios (files, streams).

short

  • Size: 2 bytes (16 bits)
  • Range: -32,768 to 32,767
short s = 32000;
          

When to use: Rarely used; mainly for memory optimization.

int (Default Integer Type)

  • Size: 4 bytes (32 bits)
  • Range: -2,147,483,648 to 2,147,483,647
int count = 500000;
          

Why it matters: Default and most commonly used integer type.

long

  • Size: 8 bytes (64 bits)
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Must end with L
long population = 7800000000L;
          

When to use: Large numbers like timestamps, IDs.

2. Floating-Point Data Types

Used to store decimal values.

float

  • Size: 4 bytes (32 bits)
  • Precision: ~7 decimal digits
  • Range: ~±3.4E38
  • Must end with f
float price = 99.99f;
          

double (Default Decimal Type)

  • Size: 8 bytes (64 bits)
  • Precision: ~15 decimal digits
  • Range: ~±1.7E308
double pi = 3.141592653589793;
          

Why it matters: Preferred for accuracy and calculations.

3. Character Data Type

char

  • Size: 2 bytes (16 bits)
  • Range: 0 to 65,535 ('\u0000' to '\uffff')
  • Stores Unicode characters
char grade = 'A';
char symbol = '@';
          

Why it matters: Supports international characters.

4. Boolean Data Type

boolean

  • Size: JVM dependent (logical size = 1 bit)
  • Values: true or false
boolean isActive = true;
          

Important Interview Note: Java specification does not define exact memory size for boolean.

Summary Table — Size & Range

Data Type Size Range / Precision Default Value
byte 1 byte -128 to 127 0
short 2 bytes -32,768 to 32,767 0
int 4 bytes -2,147,483,648 to 2,147,483,647 0
long 8 bytes -9.22E18 to 9.22E18 0L
float 4 bytes ~7 digits, ±3.4E38 0.0f
double 8 bytes ~15 digits, ±1.7E308 0.0d
char 2 bytes 0 to 65,535 '\u0000'
boolean JVM dependent true / false false

Overflow & Underflow (Important Concept)

Overflow Example

byte b = 127;
b++;   // becomes -128
          

Underflow Example

byte b = -128;
b--;   // becomes 127
          

Why it matters: Values wrap around when exceeding range.

Default Values Rule (Interview Favorite)

  • Default values apply only to:
    • Instance variables
    • Static variables
  • Local variables must be initialized
int x;      // ❌ local variable – error
static int y; // ✅ default = 0
          

Common Beginner Mistakes

  • Using byte or short unnecessarily
  • Forgetting L for long values
  • Using float instead of double
  • Assuming boolean is 1 byte
  • Ignoring overflow scenarios

Interview-Ready Answers

Short Answer

Java primitive data types have fixed sizes and ranges, ensuring platform-independent behavior.

Detailed Answer

Java defines fixed sizes for primitive data types like byte (1 byte), int (4 bytes), and double (8 bytes). These fixed sizes help maintain consistency across platforms and prevent unpredictable behavior due to hardware differences.

Key Takeaway

Java’s fixed size and range of data types ensure portability, safety, and predictable behavior. Choosing the correct data type is essential for efficient and reliable Java programs.