Java supports multiple integer types to represent whole numbers. These types differ in memory size, value range, and performance, allowing developers to choose the right one based on the data requirements.


Overview of Integer Types

Type Size Range Default Value Object Wrapper
byte 1 byte -128 to 127 0 Byte
short 2 bytes -32,768 to 32,767 0 Short
int 4 bytes -2,147,483,648 to 2,147,483,647 0 Integer
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L Long
BigInteger Varies Virtually unlimited (depends on system memory) BigInteger.ZERO BigInteger (class)

int: Standard Integer

The most commonly used integer type for general whole number operations.

int age = 25;
int marks = -100;

long: Large-Scale Integer

Used for storing very large whole numbers, such as timestamps or IDs.

long worldPopulation = 8000000000L;  // Note the 'L' suffix

short: Compact Integer

A small-range integer, typically used where memory is tight (e.g., embedded systems, large arrays).

short temperature = -200;
short port = 8080;

BigInteger: Arbitrary Precision Integer

Used when integer values exceed the range of long. Found in java.math.

import java.math.BigInteger;

BigInteger big = new BigInteger("999999999999999999999");
BigInteger result = big.multiply(BigInteger.valueOf(2));

Summary Table

Type Use Case Pros Cons
int Default for numeric values Fast, memory-efficient Limited to ~2 billion
long Timestamps, large counters Huge range Slightly more memory
short Compact data (e.g., network packets) Smaller size Rarely used in practice
BigInteger Cryptography, finance, math libraries Virtually unlimited Slower, verbose syntax

Best Practices


Example: Choosing the Right Integer Type

int studentCount = 3000;
long timestamp = System.currentTimeMillis();
BigInteger balance = new BigInteger("123456789012345678901234567890");

Use the smallest appropriate type for clarity and efficiency.


Classes
Quiz
Videos
References
Books