Java Data Types

Data type is nothing but a type of data, data can be used to declare a variables.

Data types are devided into two types

1. Primitive data types: In java there are 8 different types of primitives those are boolean, char, byte, short, int, long, float and double.

2. Non-primitive data types: In Java non-primitive data types are Classes, Interfaces, and Arrays.

Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Boolean Data Type

The Boolean data type is used to store only two possible values: true or false.

Example: Boolean ref = false

Byte Data Type

The byte is one of the primitive data type. It occupy 1-byte or 8-bit memory and its range is between -128 to 127 (inclusive). Its default value is 0.

Example: byte a = 10, byte b = -20

Short Data Type

The short is one of the primitive data type. It occupy 2-byte's or 16-bit memory. Its range is between -32,768 to 32,767 (inclusive). Its default value is 0.

Example: short s = 10000, short r = -5000

Int Data Type

The int is one of the primitive data type. It occupy 4-byte's or 32-bit memory. Its range is between -2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its default value is 0.

Example: int a = 100000, int b = -200000

Long Data Type

The long is one of the primitibe data type. It occupy 8-bytes's or 64-bit memory. Its range is between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its default value is 0.

Example: long a = 100000L, long b = -200000L

Float Data Type

The float is one of the primitive data type. It occupy 4-byte's or 32-bit memory. Its range is unlimited. Its default value is 0.0F.

Double Data Type

The double is one of the primitive data type. It occupy 8-byte's or 32-bit. Its range is unlimited. Its default value is 0.0d.

Example: double d1 = 12.3

Char Data Type

The char is one of the primitive data type. It occupy 2-byte's or 16-bit Unicode character. Its range is between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).

Example: char letterA = 'A'