Integer Data Types in Java
In Java, integer types are used to store whole numbers (without a decimal part). They are always stored in Base-2 (binary) format, because computers understand only binary digits (0 and 1).
Java represents integers using the 2’s complement system, which allows both positive and negative numbers to be stored efficiently.
General Formula (for n bits)
Range = −2ⁿ⁻¹ to (2ⁿ⁻¹ − 1)
- The leftmost bit is the sign bit (0 = positive, 1 = negative).
- The remaining bits represent the magnitude of the number.
1. byte
Syntax:
byte a;
- Size: 1 byte = 8 bits
- Base: Base-2 (binary)
- Range (formula): −2⁷ to 2⁷−1
- Range (actual): -128 to 127
- Total Values: 256
- Representation: Stored in Base-2 using 2’s complement
- Usage: Memory-efficient storage for small numbers
- Example: age, no_of_children
2. short
Syntax:
short a;
- Size: 2 bytes = 16 bits
- Base: Base-2 (binary)
- Range (formula): −2¹⁵ to 2¹⁵−1
- Range (actual): -32,768 to 32,767
- Total Values: 65,536
- Representation: Stored in Base-2 using 2’s complement
- Usage: When a larger range is needed but memory saving is still important
- Example: salary_of_fresher, max_speed
3. int
Syntax:
int a;
- Size: 4 bytes = 32 bits
- Base: Base-2 (binary)
- Range (formula): −2³¹ to 2³¹−1
- Range (actual): -2,147,483,648 to 2,147,483,647
- Total Values: ~4.29 billion
- Representation: Stored in Base-2 using 2’s complement
- Usage: Default integer type in Java, used in most calculations
- Example: population_of_country, distance_between_cities
4. long
Syntax:
long a;
- Size: 8 bytes = 64 bits
- Base: Base-2 (binary)
- Range (formula): −2⁶³ to 2⁶³−1
- Range (actual): -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Total Values: 18 quintillion+
- Representation: Stored in Base-2 using 2’s complement
- Usage: When extremely large values are needed
- All integer literals are int by default in Java.
- Suffix Rule: If a value exceeds the int range, you must add
lorLto mark it as long.
long bigValue = 2147483648L; // ✅ Correct
long wrongValue = 2147483648; // ❌ Error (out of int range)
- Example: population_of_world, distance_between_planets, phone_number
Bonus Point : BigInteger (Class)
- Package: java.math.BigInteger
- Purpose: Used when integer values go beyond the range of all primitive integer types, including long.
- Size: No fixed size (limited only by available memory).
- Operations Supported: Addition, subtraction, multiplication, division, power, modulus, GCD, etc.
- Usage: Required for cryptography, scientific calculations, or extremely large numbers.
import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
// very large numbers (greater than long)
BigInteger num1 = new BigInteger("92233720368547758070");
BigInteger num2 = new BigInteger("12345678901234567890");
// arithmetic operations
BigInteger sum = num1.add(num2);
BigInteger product = num1.multiply(num2);
System.out.println("Sum = " + sum);
System.out.println("Product = " + product);
}
}
Output:
Sum = 104579399209782325960
Product = 1133834267609527785884280001661452630
Why Do We Have Different Integer Data Types in Java?
Java provides multiple integer types (byte, short, int, long) for flexibility and efficiency
- Memory Efficiency – Smaller types (byte, short) save memory, especially in large arrays.
- Range Requirements – Different types cover different ranges of values (from small numbers to very large).
- Performance – JVM can optimize arithmetic with appropriate types.
- Code Clarity – Using the right type makes the code more meaningful (e.g., byte age, long population).
- Application Needs – Each type suits different use cases (e.g., byte in file handling, long in big calculations).
In short: Different integer types exist to balance memory, performance, range, and clarity.
Summary:
- All integer types in Java are stored in Base-2 binary form.
- Java uses the 2’s complement system for representing signed numbers.
- They differ in bit size (8, 16, 32, 64 bits), which directly affects the range of values they can store.
- Choosing the right type depends on the balance between range requirements and memory efficiency.
- Use BigInteger when numbers are too large for any primitive type.