Integers In Java

In Java, an Integer is a primitive data type used to represent whole numbers. It is denoted by the keyword int. The int type is a signed 32-bit value. It can hold integers ranging from -2,147,483,648 to 2,147,483,647.

Following are the key points to remember:

Declaration and Initialization

We can declare an integer variable by specifying the int keyword, followed by the variable name. For example:

int rollNumber;

We can also initialize the variable at the time of declaration:

int rollNumber = 10;

Arithmetic Operations

  • Integer support basic arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/).
  • The result of arithmetic operations involving integers will be an integer, and any fractional part will be truncated (rounded down).
  • Integer division (/) between two integers will yield the quotient, discarding any remainder

Following is an example of using the int data type in a Java program that sums the two variables:

public class Main {
  public static void main(String[] args) {
    int x = 10;
    int y = 20;
    int sum = x + y;
    System.out.println("The sum of x and y is: " + sum);


The sum of x and y is: 30

Integer Overflow and Underflow

  • Since int is a fixed-size data type, it has a range of representable values. If an arithmetic operation results in a value outside this range, it will cause an overflow or underflow.
  • Overflow occurs when a value exceeds the maximum representable value, while underflow occurs when a value goes below the minimum representable value. These operations are considered undefined behavior in Java.

Integer Literals

  • Integer literals can be specified in Java using decimal, octal, or hexadecimal notation.
  • Decimal literals are represented in base 10 (e.g., 10, 123).
  • Octal literals are represented with a leading 0 followed by octal digits (e.g., 012, 077).
  • Hexadecimal literals are represented with a leading 0x or 0X, followed by hexadecimal digits (e.g., 0xA, 0xFF).


  • The decimal equivalent of the hexadecimal value 0xA is: 10

You can test it by directly printing this value.

System.out.println(0xA); // 10
  • Similarly, the decimal equivalent of the hexadecimal value 0xFF is: 255. You can use lowercase values. Like: 0xff the output will be the same.

Integer Wrapper Class

  • Java also provides a corresponding wrapper class for the int primitive type called Integer.
  • The Integer class provides useful methods to perform operations on integers, convert strings to integers, and vice versa.

If you need to represent larger numbers or require more precision, you can use other numeric types in Java, such as long (for larger integers) or double (for decimal numbers).

Frequently Asked Questions

What are integers in Java?

Integers are a fundamental data type in Java used to represent whole numbers, both positive and negative, without fractional parts.

How many types of integers are there in Java?

Java provides four integer types: byte, short, int, and long, which vary in size and can hold different ranges of values.

What is the default value of an uninitialized integer variable in Java?

The default value for uninitialized integer variables in Java is 0.

What is integer overflow in Java?

Integer overflow occurs when an operation on integers results in a value that exceeds the maximum range of the data type, causing unexpected behavior.

How can I convert other data types to integers in Java?

You can use type casting or the appropriate conversion methods, such as Integer.parseInt() for strings, to convert other data types to integers in Java.