Home› Java Programming Language› / Programs / Java Program to Demonstrate the int Data Type
🚀 Programming Example

Java Program to Demonstrate the int Data Type

2,745 Views
Java Java 8 or later
Beginner
Demonstration

Learn how to declare, initialise and print int variables in Java. This program prints the range of int using Integer.MAX_VALUE and Integer.MIN_VALUE. It also shows that int overflow wraps around without any error, and how to use underscores to make large numbers easier to read.

Before You Begin

What you will learn

  • Declare and initialise variables of the int data type
  • Know that int uses 4 bytes (32 bits) and stores values from -2,147,483,648 to 2,147,483,647
  • Print the range using Integer.MAX_VALUE and Integer.MIN_VALUE
  • Understand that int + int gives an int, so no cast is needed
  • Recognise integer overflow and know that Java doesn't report it as an error
  • Use underscores in numeric literals to improve readability
  • Know when to switch from int to long

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • What a variable is and how to declare one
  • Basic idea of the byte and short data types
No previous program
No next program

📌 Information & Algorithm

  • Int data type is a 32-bit signed two's complement integer.

  • Minimum value is - 2,147,483,648 (-2^31)

  • Maximum value is 2,147,483,647(inclusive) (2^31 -1)

  • Integer is generally used as the default data type for integral values unless there is a concern about memory.

  • The default value is 0

  • Example: int a = 100000, int b = -200000

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac IntDataType.java
java IntDataType

💻 Program Code

public class IntDataType {

    public static void main(String[] args) {

        // Declare and initialise int variables
        // int is the default type for whole numbers in Java
        int a = 10000;
        int b = -5000;

        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Range of the int data type
        System.out.println("Max int value = " + Integer.MAX_VALUE);
        System.out.println("Min int value = " + Integer.MIN_VALUE);

        // int + int stays an int, so no cast is needed
        int sum = a + b;
        System.out.println("sum = " + sum);

        // Overflow: going past the maximum wraps around silently
        int overflow = Integer.MAX_VALUE + 1;
        System.out.println("Integer.MAX_VALUE + 1 = " + overflow);

        // Underscores make large numbers easier to read (Java 7+)
        int population = 1_500_000;
        System.out.println("population = " + population);
    }
}

Sample Input

No input required

🖥 Program Output

                                a = 10000
b = -5000
Max int value = 2147483647
Min int value = -2147483648
sum = 5000
Integer.MAX_VALUE + 1 = -2147483648
population = 1500000
                            

📘 Explanation

Overview

int is the most commonly used integer data type in Java. It stores whole numbers in 4 bytes (32 bits) of memory, with a range of -2,147,483,648 to 2,147,483,647. Every whole-number literal you write, such as 10000, is an int by default.

Use int for everyday whole numbers such as counters, loop variables, ages and marks. Switch to long only when values can go beyond about 2.1 billion.

Step-by-Step Explanation

1

Declare and Initialise int Variables

The default type for whole numbers.

int a = 10000;
int b = -5000;

int is the data type, and a and b are the variable names. No suffix is needed, because whole-number literals are already int.

2

Print the Values

Text and values joined with +.

System.out.println("a = " + a);
System.out.println("b = " + b);

The + operator joins the label and the value, so the output is a = 10000 and b = -5000.

3

Range of int

The largest and smallest values.

System.out.println("Max int value = " + Integer.MAX_VALUE);
System.out.println("Min int value = " + Integer.MIN_VALUE);

Integer is the wrapper class for int. With 32 bits, an int can store $$2^{32}$$ different values, from $$-2^{31}$$ to $$2^{31} - 1$$.

4

int + int Stays an int

No cast is needed.

int sum = a + b;
System.out.println("sum = " + sum);

Unlike byte and short, adding two int values gives an int. So the result 5000 is stored directly, with no cast.

5

Silent Overflow

Going past the maximum wraps around.

int overflow = Integer.MAX_VALUE + 1;
System.out.println("Integer.MAX_VALUE + 1 = " + overflow);

This compiles and runs without any error, but the result is wrong. The value goes past the maximum and wraps around to the minimum: $$2147483647 + 1 \rightarrow -2147483648$$ This is a common source of bugs in calculations with large numbers.

6

Underscores in Numbers

Easier to read, same value.

int population = 1_500_000;
System.out.println("population = " + population);

From Java 7, you can place underscores between digits. The compiler ignores them, so 1_500_000 is the same as 1500000, and the underscores don't appear in the output.

int at a Glance

Property Value
Size 4 bytes (32 bits)
Range -2,147,483,648 to 2,147,483,647
Default value (fields) 0
Literal suffix None (default for whole numbers)
Wrapper class Integer

Java Integer Types Compared

Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes About ±9.22 × 1018

Output

a = 10000
b = -5000
Max int value = 2147483647
Min int value = -2147483648
sum = 5000
Integer.MAX_VALUE + 1 = -2147483648
population = 1500000

Common Mistakes

Writing a literal that's too large int big = 3000000000; gives the error integer number too large. Use long big = 3000000000L; instead.
Ignoring overflow int result = 50000 * 50000; compiles but gives a wrong negative number, because the real answer is bigger than the int range.
Putting underscores in the wrong place _1000, 1000_ and 1_.5 aren't allowed. Underscores can only go between digits.
Good practice When overflow would be a serious bug, use Math.

Complexity

Time: O(1)

Space: O(1)

No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.