Integer: byte, short, int, and long data types in Java

By: Abinaya Emailed: 1729 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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. Java manages the meaning of the high-order bit differently, by adding a special "unsigned right shift" operator. Thus, the need for an unsigned integer type was eliminated.

The width of an integer type should not be thought of as the amount of storage it consumes, but rather as the behavior it defines for variables and expressions of that type. The Java run-time environment is free to use whatever size it wants, as long as the types behave as you declared them. In fact, at least one implementation stores bytes and shorts as 32-bit (rather than 8- and 16-bit) values to improve performance, because that is the word size of most computers currently in use.

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

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

Let's look at each type of integer.


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;

"Endianness" describes how multibyte data types, such as short, int, and long, are stored in memory. If it takes 2 bytes to represent a short, then which one comes first, the most significant or the least significant? To say that a machine is big-endian, means that the most significant byte is first, followed by the least significant one. Machines such as the SPARC and PowerPC are big-endian, while the Intel x86 series is little-endian.


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. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. Any time you have an integer expression involving bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done.

The int type is the most versatile and efficient type, and it should be used most of the time when you want to create a number for counting or indexing arrays or doing integer math. It may seem that using short or byte will save space, but there is no guarantee that Java won't promote those types to int internally anyway. Remember, type determines behavior, not size. (The only exception is arrays, where byte is guaranteed to use only one byte per array element, short will use two bytes, and int will use four.)


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. This makes it useful when big, whole numbers are needed. For example, here is a program that computes the number of miles that light will travel in a specified number of days. 

// Compute distance light travels using long variables.
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:

In 1000 days light will travel about 16070400000000 miles.

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


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

A useful article. It answered my question about when you should "just use int" even when the numeric range needed is so small that a short or byte would be enough. Answer: "just use int" except in arrays if you want to save memory". Thanks!

View Tutorial          By: Kris Heidenstrom at 2011-05-28 04:15:05
2. View Comment

Great Article! Thanks

View Tutorial          By: Rachid at 2011-06-21 13:01:39
3. View Comment

Don't forget the native type char, which is a 16 bit unsigned integer.

View Tutorial          By: Ron Stern at 2011-09-18 17:24:12

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Abinaya
How to compile a Java program - javac
The clone() Method in Java
Data Types in Java
JSP Example to connect to MS SQL database and retrieve records
faces-config.xml to DirectTraffic in the JSF Application
Enabling Expression Language Evaluation in JSP
Using malloc() Function in C
ActionErrors and ActionError in Struts
Open, Creat, Close, Unlink system calls sample program in C
Structures and Functions in C
Introduction to JSP expression language
Multi-dimensional Arrays in C (Explained using date conversion program)
A C program similar to grep command in UNIX
Type Conversions in C (String to Integer, isdigit() etc)
Basics of C

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
What is Java?
Method Overloading (function overloading) in Java
wait(), notify() and notifyAll() in Java - A tutorial
The Basic Structure of a Simple Java program
Method Overriding in Java
XML and Java - Parsing XML using Java Tutorial
Recursion in java
How to use ArrayList in Java
Vector example in Java
FileReader and FileWriter example program in Java
Simple Port Scanner application using Java
Stack example in Java - push(), pop(), empty(), search()
Using StringTokenizer in Java
instanceof sample program in Java
Most Emailed Articles (in Java)
Vector in Java - Sample Program
Applet Notinited : Applet xxxxxxxxxxxx notinited
Sample using loadLibrary( ) to call Native methods in Java
equals( ) Versus == in Java
The modulus Operators
The Basic Structure of a Simple Java program
Calculate average sale of the week in Java
MultiLevel Inheritance sample in Java
Taking the size of an Array at runtime & generate random numbers to populate the Array
Configuring a DataSource in Tomcat
What is JasperReports?
java.lang.reflect package
Sending Email from Java application (JavaMail sample program)
instanceof operator example in Java
CharArrayWriter sample program in Java