Java Integer Data Type and Range

Java defines these four integer types:

  1. byte
  2. short
  3. int
  4. long

All of these are signed, positive, and negative values.

Java doesn't support unsigned, positive-only integers. Lots other computer languages support both signed and unsigned integers.

Anyhow, Java's designers felt that unsigned integers were unnecessary. Specifically, they felt that the concept of unsigned was used largely to specify the behaviour of high-ordered bit, which defines the sign of an integer value. Java handles the significance of high-ordered bit differently, by adding a special "unsigned right shift" operator. Therefore, the requirement for an unsigned integer type was eliminated.

The width of an integer type should not be intended of as the amount of storage it consumes, but instead as the behaviour it defines for the variables and expressions of that data type. However, the Java run-time environment is always free to use whatever the size it wants, as long as the types behave as you declared them.

Integer Width and Range

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

Name Width Range
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 -263 to 263 -1
int 32 -2,147,483,648 to 2,147,483,647 -231 to 231 -1
short 16 -32,768 to 32,767 -215 to 215 -1
byte 8 -128 to 127 -27 to 27 -1

Now, lets take a look at each type of integer.

byte

byte is the smallest integer type. This is signed 8-bit data type which has a range from -128 to 127. Variables of type byte are especially useful when you are working with stream of data from network or file. They are also useful when you are working with raw binary data which may not be directly compatible with other built-in types of Java.

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

byte b, c;

short

short is a signed 64-bit type. It has range from -32,768 to 32,767. It is the least-used type. Here are some examples of short variable declarations :

short s;
short t;

int

As you know that the most used integer data type is int. It is a signed 32-bit type having range from -2,147,483,648 to 2,147,483,647.

In addition to other uses, int type variables are commonly employed to control the loops and to index arrays. Even though you might think that using a byte or short would be more efficient than using an int in situations in which the larger range of an int data type isn't needed, this may not be the case. The reason is that when byte and short values are used in an expression, then they are promoted to int when the expression is evaluated. (You will learn about Type Promotion in two other chapters i.e., automatic type promotion and type promotion rules ). Therefore, int is the best choice when an integer is needed.

long

long is a singed 64-bit type and is useful for those occasions where an int data type is not large enough to hold the desirable value. The range of a long is quite large. This makes it helpful when big, whole numbers are needed. For instance, here is a program that computes the number of miles, light will travel in a given number of days :

/* Java Program Example - Java Integer Data Types
 * Compute distance light travels using long variables
 */

public class JavaProgram
{   
    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 ligh will travel about " + distance + " miles.");
        
    }
}

When the above Java program is compile and run, it will produce the following output :

java integer types

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

Java Online Test


« Previous Tutorial Next Tutorial »