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 l or L to 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

  1. Memory Efficiency – Smaller types (byte, short) save memory, especially in large arrays.
  2. Range Requirements – Different types cover different ranges of values (from small numbers to very large).
  3. Performance – JVM can optimize arithmetic with appropriate types.
  4. Code Clarity – Using the right type makes the code more meaningful (e.g., byte age, long population).
  5. 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.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar