by Dinesh Thakur Category: Data Type

A variable in a program occupies some space in computer's memory where some value is stored. A value to be stored may be an integer, floating point number, a character or a string. However, now the question arises that how much memory is allocated to these values and what is the range of values that can be stored in it as different types of values don't occupy same space in memory. So to store these values, we require different data types depending on the needs of the application. The data type determines how much memory is allocated to store data and what operations can be performed on it. The Data type also defines the values that a variable can takes.

Java has statically typed language (do type checking at compile-time instead of at run-time).In Java, some of the predefined (already defined) data types are part of the programming language and constants (or an identifier), and variables are defined as certain data types before you can use it.

In Java, the data types can be classified into two broad categories:

Primitive type (or built-in type or value type) and
Reference type (non-primitive or derived type).

Difference between primitive and reference data type in java

The main difference between the primitive type and reference type is that the memory location associated with the primitive type variable contains the actual value of the variable. On the other hand, the memory location associated with the reference type variable contains an address that indicates the memory location of the actual object.

In Java, there are eight primitive data types. Out of the eight primitive types, six are for numbers, one for character and one is for boolean values. Of the six number types, four are types of integers, and two are types of floating-point numbers.
The reference type includes array, interface and class.

primitive and reference data types

Primitive Data Types

Primitive data types definition: That are non grouped pieces of data which can have only one value at a time. They are the simplest built in forms of data in Java. Other data types are made up of combination of primitive data types. Java initializes all primitive data types to default values if an initial value is not explicitly specified by the programmer. Integer and floating-point variables are initialized to 0. The char data type is initialized to null and Boolean data types are set to false. The example of primitive data type

types of primitive data types

The 8 primitive data types byte, short, int, long, float, double, boolean and char are called primitive data types , as they are built into the Java language at low level .

Reference Data Types

Reference data types are made by the logical grouping of primitive data types. These are called reference data types because they contain the address of a value rather than the value itself. Arrays, objects, interfaces, enum etc. are examples of reference data types.

Intergral Types

The integral types consists of integer types and character types.

Integer Data Types

Integer types are used to store whole numbers without a fractional part. In Java, there are four integer types: byte, short, int and long. These four types differ only in the number of bits and therefore in the range of values. Each data type can store both positive as well as negative values. However, unlike C/C++, Java does not support unsigned integers.
The following table enlists the four integer types, their size (i.e. number of bits it occupies in memory) and their minimum and maximum values.

Size of primitive data types in java



Minimum Value                     

Maximum Value


8 bits   








32 bits
















Possible values ​​are two - true or false









The range of integer types lie in the range -2x-1to +2x-1-1 where x is the total number of bits used to store the data type. One has been subtracted from x as one bit is reserved for the sign bit and the remaining for the magnitude. Also, we have subtracted 1 from the upper limit of the range because 0 is included.

byte: In certain applications, you may not need integers as large as the standard int type provides. In such cases, Java provides two smaller integer types byte and short. The byte data type is an 8-bit (1 byte) signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
short: The short data type is a 16-bit (2 byte) signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

The byte and short types are mainly intended for specialized applications, such as low-level file handling, or for large arrays where saving the memory space is important. We could have used this instead of int, but you can only use the short variable if you know  100% that your values will not exceed that range. 

     A variable age of byte type and pageCount of short type can be declared as follows.
byte age;          // The byte type variable 'age' can store value within range -128 to +127
short pageCount;        // The int type variable 'pageCount' can stores values within range -32768 to +32767

int: The int data type is a 4-bytes(32 bits) signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else.

A variable length of int type can be declared as follows.
int length;
long: The long data type is a 8 bytes (64-bits) signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036, 854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

A variable distance of long integer type can be declared as follows.
long distance;
Each integer type has a corresponding wrapper class Byte, Short, Integer and Long.

In Java, the size of integer types is specified by the language and is always the same regardless of what kind of computer a program runs on. This make the Java programs to produce the same results on all the computer. This is a huge improvement over the C/C++ language which let the compilers for different platforms determine the optimum size for integer data types. As a result, C/C++ program written or tested on one type of computer might not produce the same result on another computer. This is also true for other primitive types.

char Data Types

Type char is basically a 16-bit unsigned integer that represents a Unicode value. This means that a total of 216 or 65536 different unicode characters can be represented corresponding to the integer values 0 to 65535. Each printable and nonprintable character has a Unicode value. Because Java stores all characters as Unicode, it can be used with virtually any written language in the world.

A character variable choice of char type can be declared as follows,
char choice;

Floating-Point Data Types

The integer types do not provide the facility to represent decimal number so whenever you want to represent a number with a decimal such as 3.142, use a floating-point data type. If you try to use an integer type to store a number with fractional part, the fractional part will simply be discarded. So, Java provides two primitive floating point types: float and double to store numbers with fractional parts. Out of the two, double is most commonly used because all the math functions in Java class library use double values.

floating point types

NOTE: Both float and double follow the IEEE-754 format.

float: The float data type requires 4 bytes (32-bits) for their storage. Their digits of precision (numbers of digits after decimals point) are 7 and range lies between -3.4 E38 (-3.4x1038) to +3.4E38 (+3.4x1038). This data type is normally used to represent quantities which inheritally cannot be represented by integers. For example : height of a person, average of number of
values etc.

A variable average of float data type can be declared as follows,

float average;

Double: Double variables are used when we want to store much larger or smaller values than the maximum and minimum allowable in a standard variable. The double data type uses 8 bytes (M-bits) for their storage. Their digits of precision are 15 and range lies between -1.7E308 to +1.7E308. A variable sunDistance of double type can be declared as follows,

double sunDistance;

NOTE: Since the double type is twice as big as the float type, so double is known as double precision and float is known as single precision.

boolean Data Types

Boolean type is used to represent true/false values. For example: whether light is on, container is empty etc. It got its name from George Boole, an English Mathematician who invented the concept of Boolean Algebra. In Java, boolean type is represented using boolean keyword. The boolean data type can only hold two values: true and false. The values true and false are literals. Booleans are typically used for design making and for controlling the order of execution of a program.
You can declare a variable flag of type boolean as follows.
boolean flag;
NOTE: A boolean is not an integral type as in C++ and integer values cannot be used in place of boolean.

double Data Types

The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.