Java Integer types

We’ll cover the following

  • Introduction
  • Note
  • byte
  • short
  • int
  • long


Java defines four integer types: byte, short, int, and long. All of these are signed, positive
and negative values. Java does not support unsigned, positive-only integers. Many other
computer languages, including C/C++, support both signed and unsigned integers.
However, Java’s designers felt that unsigned integers were unnecessary. Specifically,
they felt that the concept of unsigned was used mostly to specify the behavior of the
high-order bit, which defined the sign of an int when expressed as a number.

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

intA 32-bit (4-byte) integer value-2,147,483,648 to 2,147,483,647
byteAn 8-bit (1-byte) integer value-128 to 127
short A 16-bit (2-byte) integer value-32,768 to 32,767
longA 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, since it is defined as having its high byte first (called big-endian
format). This type is mostly applicable to 16-bit computers, which are becoming
increasingly scarce.
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.The range of a long is quite large.

For example, here is a program that computes the number of miles that light will travel in a specified number of days:

package JAVA.Basic;

// Compute distance light travels using long variables.
public class Light {
    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 = 1000; // 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:

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Primitive Data Types

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


Leave a Reply

Your email address will not be published.