The int data type is the most commonly used integer type in Java. Whenever a program needs to store a whole number such as an age, quantity, score, count, or year, int is usually the first choice.
It provides a practical balance between range, memory usage, and performance. More importantly, Java's arithmetic expressions naturally work with int, which makes it the standard integer type for everyday programming.
What Is the int Data Type?
The int data type is a signed 32-bit integer type. It can store whole numbers from -2,147,483,648 to 2,147,483,647.
int age = 25; int marks = 450; int quantity = 100;
The int type is suitable for a very large number of common programming tasks because its range is large enough for most ordinary whole-number calculations.
Key Point: Java's int is a signed 32-bit integer with a range of -2,147,483,648 to 2,147,483,647.
Why Is int So Common?
Java provides several integer types, but int is the natural default for most integer operations. It is large enough for everyday values while remaining compact enough for general-purpose use.
For example, consider a student management system. Values such as student IDs, marks, subject counts, semester numbers, and quantities can often be represented comfortably using int.
int studentId = 1025; int totalMarks = 475; int subjectCount = 6;
Syntax of int
int variableName = value;
For example:
int age = 22; int year = 2026; int salary = 50000;
The assigned value must be a valid integer that fits within the int range.
Range of int
An int uses 32 bits and is signed. Its range can be expressed mathematically as -231 to 231 - 1.
| Property | Value |
|---|---|
| Data Type | int |
| Category | Integer |
| Size | 32 bits |
| Minimum Value | -2,147,483,648 |
| Maximum Value | 2,147,483,647 |
| Default Value for a Field | 0 |
| Signed | Yes |
Integer Literals Are int by Default
When you write a whole-number literal without a suffix, Java normally treats it as an int.
int number = 100; int value = 50000;
This is one reason int feels so natural in Java. You do not need a special suffix for ordinary integer literals.
Remember: A whole-number literal such as 100 is an int literal by default.
Maximum and Minimum Values
The largest value that an int can store is 2,147,483,647.
int maximum = 2147483647;
The smallest value is -2,147,483,648.
int minimum = -2147483648;
Trying to store a value outside this range in an int literal can cause a compilation error.
int Arithmetic
Arithmetic operations involving int values generally produce an int result.
int a = 20; int b = 10; int sum = a + b; int difference = a - b; int product = a * b; int quotient = a / b;
This makes int convenient for counters, calculations, indexes, and other common programming operations.
Integer Division
A particularly important behavior is integer division. When two integers are divided, Java performs integer division and discards the fractional part.
int result = 10 / 3; System.out.println(result);
The result is 3, not 3.333....
If you need a decimal result, at least one operand must participate in floating-point arithmetic.
double result = 10.0 / 3; System.out.println(result);
Common Interview Point: 10 / 3 produces 3 because both operands are integers.
int Overflow
An int has a fixed range. If an arithmetic operation goes beyond its maximum or minimum value, the result can wrap around.
int number = 2147483647; number++; System.out.println(number);
The result becomes -2147483648. The value has wrapped from the maximum positive int value to the minimum negative value.
This can become a serious bug in applications involving large counters, timestamps, financial calculations, file sizes, or other values that can grow unexpectedly.
int and Other Integer Types
| Type | Size | Minimum | Maximum | Typical Use |
|---|---|---|---|---|
| byte | 8 bits | -128 | 127 | Compact small values |
| short | 16 bits | -32,768 | 32,767 | Compact medium-range values |
| int | 32 bits | -2,147,483,648 | 2,147,483,647 | General-purpose integers |
| long | 64 bits | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | Very large integers |
Converting int to Other Types
An int can be automatically converted to a larger compatible numeric type because the destination can represent every possible int value.
int number = 500; long largeNumber = number; float decimalNumber = number; double preciseNumber = number;
However, converting an int to a smaller type such as short or byte requires an explicit cast.
int number = 100; byte smallNumber = (byte) number; short mediumNumber = (short) number;
int with char
A char can participate in numeric operations because Java represents a char as a 16-bit unsigned UTF-16 code unit.
char letter = 'A'; int code = letter; System.out.println(code);
The variable code receives the numeric value associated with the character. This is one reason int is central to many Java expressions involving characters.
Using int in Loops
The int type is extremely common in loops, especially when working with indexes and counters.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This is one of the most common uses of int in Java programs.
Using int with Arrays
Array indexes are represented using integer values, making int a natural choice when accessing array elements.
int[] marks = {85, 90, 78, 92};
int index = 2;
System.out.println(marks[index]);
Here, index stores the position used to access an element from the array.
When Should You Use int?
- When storing ordinary whole numbers.
- For loop counters and indexes.
- For quantities, scores, ages, counts, and similar values.
- When the expected value fits within the int range.
- For general-purpose integer arithmetic.
If your application may need values larger than the int range, consider using long instead.
Common Beginner Mistakes
- Forgetting that integer division removes the fractional part.
- Assuming int can store unlimited whole numbers.
- Ignoring integer overflow in calculations involving very large values.
- Trying to assign an int directly to byte or short without casting.
- Using int for decimal calculations and expecting fractional results.
Best Practices
- Use int as the default integer type for ordinary calculations.
- Use long when the expected range may exceed the int limits.
- Use double when calculations require decimal values.
- Check for possible overflow when working with large values.
- Use meaningful variable names to make integer values easier to understand.
Interview Insights
Question: What is the size and range of int in Java?
Answer: int is a signed 32-bit integer type with a range from -2,147,483,648 to 2,147,483,647.
Question: What is the result of 10 / 3 when both values are int?
Answer: The result is 3 because integer division discards the fractional portion.
Question: Why is int commonly used in Java?
Answer: It provides a useful 32-bit range and is the natural type for most integer literals and arithmetic operations.
int Data Type: Quick Revision
| Property | Details |
|---|---|
| Type | int |
| Category | Integer |
| Size | 32 bits |
| Minimum | -2,147,483,648 |
| Maximum | 2,147,483,647 |
| Default Field Value | 0 |
| Signed | Yes |
| Integer Literal Default | int |
| Example | int age = 25; |
The int data type is the workhorse of Java's integer system. It is simple enough for beginners, powerful enough for most everyday calculations, and deeply connected to Java's arithmetic and control-flow operations. If you are unsure which integer type to choose for a normal whole-number value, int is usually the sensible starting point.
