Java Integer types


An integer is a whole number, a number with no fractional or decimal portion.


All of these are signed, positive and negative values.

Java does not support unsigned, positive-only integers.

The width and ranges of these integer types vary widely, as shown in this table:

Type Explanation Range
intA 32-bit (4-byte) integer value -2,147,483,648 to 2,147,483,647
byte An 8-bit (1-byte) integer value -128 to 127
short A 16-bit (2-byte) integer value -32,768 to 32,767
long A 64-bit (8-byte) integer value -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

The most commonly used integer type is int. This type uses 4 bytes to store an integer value that can range from about negative 2 billion to positive 2 billion.

long is a 64-bit integer that can hold numbers ranging from about negative 9,000 trillion to positive 9,000 trillion.

In some cases, you may not need integers as large as the standard int type provides. For those cases, Java provides two smaller integer types: byte and short.

The short type represents a two-byte integer, which can hold numbers from -32,768 to +32,767, and the byte type defines a single-byte integer that can range from -128 to +127.

We should stick to int and long most of the time. Also, use long only when you know that you're dealing with numbers too large for int.


In Java, the size of integer data types is specified by the language and is the same regardless of what computer a program runs on.

Java allows you to promote an integer type to a larger integer type. Java allows the following, for example:

int xInt; 

long yLong; 

xInt = 32; 

yLong = xInt;

Here you can assign the value of the xInt variable to the *yLong variable because yLong is larger than xInt. Java does not allow the converse, however:

int xInt; 

long yLong; 

yLong = 32; 

xInt = yLong;

The value of the yLong variable cannot be assigned to the xInt because xInt is smaller than yLong.

Because this assignment may result in a loss of data, Java doesn't allow it.

If you need to assign a long to an int variable, you must use explicit casting.


You can include underscores to make longer numbers easier to read.

Thus, the following statements both assign the same value to the variables xLong1 and xLong2:

long xLong1 = 12345678; 

long xLong2 = 12_345_678;


The smallest integer type is byte.

This is a signed 8-bit type that has a range from -128 to 127.

Variables of type byte are especially useful when you're working with a stream of data from a network or file.

They are also useful when you're working with raw binary data that may not be directly compatible with Java's other built-in types.

Byte variables are declared by use of the byte keyword. For example, the following declares two byte variables called b and c:

byte b, c;


short is a signed 16-bit type.

It has a range from -32,768 to 32,767.

It is probably the least-used Java type. Here are some examples of short variable declarations:

short s;
short t;


The most commonly used integer type is int.

It is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647.

The variables of type int are commonly employed to control loops and to index arrays.

When byte and short values are used in an expression, they are promoted to int when the expression is evaluated.

Therefore, int is often the best choice when an integer is needed.


long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.


// Compute distance light travels using long variables.
public class Main {
  public static void main(String args[]) {
    int lightspeed;
    long days;
    long seconds;
    long distance;

    // approximate speed of light in miles per second
    lightspeed = 186000;

    days = 10000; // specify number of days here

    seconds = days * 24 * 60 * 60; // convert to seconds

    distance = lightspeed * seconds; // compute distance

    System.out.print("In " + days);
    System.out.print(" days light will travel about ");
    System.out.println(distance + " miles.");

This program generates the following output:

In 1000 days light will travel about 160704000000000 miles.

Clearly, the result could not have been held in an int variable.