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.
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.