Primitive Data Types In JAVA With Examples

When we say Data Type think of type of information you want to store in variables like integer, character, decimal etc. Primitive means this data type are pre-defined by JAVA language.

JAVA has 8 reserved keyword for primitive data type for assigning 8 different type of information based on value (type of information) and byte (memory or space). For example int is used for a 32-bit (4-byte) integer value, char for 16-bit character, boolean for true or false value, short for 16-bit (2-byte) integer value etc.

In JAVA this data type must be declared with variables when creating them. This helps compiler to ensure we are inserting the right type of data in variables which we have assigned with a particular data type.

Primitive Data Types With Examples:

Lets try to understand the concept with examples of int data type which is used for assigning integer (number without a decimal point). Suppose we want to store initial value 5 in x variables:

int x = 5;

But what if we want to store 5.2 in integer data type:

int x = 5.2;

The compiler will throw error because we are trying to store decimal value in integer data type.

Important Note: One more important thing to note is that data type also assign the limit of space that can be store in variables. In computing terms there is always a limit of value minimum and maximum that can be stored. For example int can store minimum value of -231 and a maximum value of 231-1. If you try to store higher value than 231-1 or lesser value than -231 then compiler will throw an error.

Suppose we try to insert value 2,147,483,648 in integer variable:

int x = 2,147,483,648;

The compiler will give an error because we are trying to store higher value than 231-1. If you want to store that particular value then you have to use long data type instead of int. This concept will make more sense as you proceed in this article where we will discuss 8 different Primitive data types in JAVA.

Different Types Of Data Type In JAVA:

In JAVA there are total of 8 different primitive data types. Primitive types means this data types are predefined by JAVA language and has a reserved keyword.

Data Types In Java
Below are the 8 different primitive data types:


It is a 8-bit (1-byte) integer value which has a minimum value of -128 and a maximum value of 127. Byte examples:

byte x = 120;

Suppose if we try to store -200 in byte data type:

byte x = -200;

Compiler will throw an error for exceeding the limits of memory allocated for byte type.


It is a 16-bit (2-byte) integer value which has a minimum value of -32,768 and a maximum value of 32,767. Example for short data type:

short x = 30000;


It is a 32-bit (4-byte) integer value which has a minimum value of -231 and a maximum value of 231-1. int is the most common, preferred and widely used data type in Android. Example for int data type:

int x = 1245678;


It is a 64-bit (8-byte) integer value which has a minimum value of -263 and a maximum value of 263-1. This data type is also preferred in Android for storing very large value like game high score which can be in billions. Example for long data type:

long x = 12345987609L;

Remember to use either ‘l’ or ‘L’ in long data type after the value because it tells the computer we are storing value in long data type.


It is a 16-bit character using the Unicode encoding scheme has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’. Example of char data type:

char myChar = ‘0’;

In char we use single quote for representing character.

Important Note: In char data type we can only store one character, number, special character or Unicode character. To store more than one character we use String. We will discuss basics of string in this article after finishing eight primitive data type.

Lets try to put more than one character in char data type:

char myChar = ‘AB’;

You will get an error “too many character”.


It is a single-precision 32-bit (4-byte) floating-point value. Example for float data type:

float myNumber = 5.25f;

Important Note: Put f after float number because it is good practice and by default a decimal number in JAVA is assume as a double.


It is a double precision 64-bit (8-byte) floating-point value. Example of double data type:

double myNumber = 5.234d;


double myNumber = 5.234;

Both are accepted as by default decimal number is assume as double in JAVA. But prefer putting d because it is considered as good practice in good coding.


boolean data type has only two possible values: true and false. It is very useful for conditional logic. Example of Boolean data type:

boolean isEven = true;


First of all String is not a primitive data type(predefined in JAVA language) but very important to learn as it is used so commonly that it can be considered as 9th data type in JAVA. It is basically a class.

Important Note: Here we are only sharing basic of String so that you can understand the next topic in JAVA where we will use it. Just want to add a notice here, String is a big topic and full tutorial on String will be added later.

What Is String:

String is a sequence of characters. As we saw previously char is limited to store just one character or Unicode character while String can store sequence of characters.

String x = “This is a String”;

Here we use double quote to represent a string which is storing characters.

Leave a Reply

Your email address will not be published. Required fields are marked *