Basic Data Types in Java with Examples

In order to write a program serving a specific purpose, we require some memory dedicated to its computation. As a programmer, one needs to identify that memory. For this purpose, there are 2 prerequisites.

  1. Inform the operating system, on how much memory is to be allocated.

  2. Name the memory location for easy identification.

For pre-requisite 1, there is a concept of variables and their data type, which we will be discussing further.

Variable is a memory location used to store the values of our purpose. Upon creation, memory is allocated to the variable.

It is the data type, which defines how much memory is to be allocated. For our end utility, it defines the type of data that can be stored in the variable. In real life, we deal with various data types, like

Total Number of Students: 13

Height: 5.25 ft

Time: 17:15 hrs / 05:15 PM

Name: Ram Chandra

Percentage: 87.23 %

Area of Cube: 123.432 sq unit

and many more.           

Java is statically typed, so we need to declare every variable before actually using it. Declaring a variable has the format is:

<data type> <name for variable> = <value>;

We can choose to give a value to the variable as well while declaring, else the variable holds its default value. We will get a clearer vision of it, in the upcoming minutes.

We can broadly divide the data type of Java into 2 units:

  1. Primitive Datatypes
  2. Non-Primitive/Object/Reference Data types

Primitive data types

By primitive data types, we refer to those data types which are pre-defined in Java. These are the most basic data types and serve as a building block for frequently used for processing. There are eight primitive data types available in Java. They are listed below:

  • boolean
  • char
  • byte
  • short
  • int
  • long
  • float
  • double

Let us look into the abovementioned primitive data types in detail.

boolean

boolean data type is used to store data that can have at max 2 values i.e true and false. Its size is 1 bit. We often encounter situations, where we need to store yes/no, true/false kind of data. In such situations, we would use the boolean data type.

Default Value: false

Example:

boolean married = true; // It means married holds true value
boolean minor;          //Since no value is mentioned, minor takes the default value false.

char

char data type is used to store any 16-bit Unicode character. Its size is 2 byte. The value ranges from 0(‘\u0000’)  to 65535(‘\uffff’) both inclusive.

Default Value: ‘\u0000’

Example:

char gender = ‘M’; // It means gender holds ‘M’ value
char myChar;       //Since no value is mentioned, myChar takes the default value ‘\u0000’

byte

byte data type is used to store 8-bit signed two’s complement integer. Its size is 1 byte. The value ranges from -128 to 127 both inclusive. The utility of this data type is very limited. It can be used as a memory saving alternative to int(size 4 byte) in case of large arrays.

Default Value: 0

Example:

byte myByte = 101;   // myByte holds the value 101
byte negByte = -101; // negByte holds the value -101
byte defByte;        // defByte holds the default value i.e 0

short

short data type is used to store 16-bit signed two’s complement integer. Its size is 2 byte. The value ranges from -32768 to 32767 both inclusive. Similar to byte, it can be used as an alternative for memory saving to int(size 4 byte) in case of large arrays.

Default Value: 0

Example:

short myShort = 1234;    // myShort holds the value 1234
short negShort = -11111; // negShort holds the value -11111
short defShort;          // defShort holds the default value i.e 0

int

int data type is used to store 32-bit signed two’s complement integer. Its size is 4 byte. The value ranges from - 2,147,483,648 to 2,147,483,647 both inclusive. It is the default choice for every programmer when dealing with integral value unless the memory is to be concerned.

Default Value: 0

Example:

int myInt = 112321;   // myInt holds the value 112321
int negInt = -100000; // negInt holds the value -100000
int defInt;           // defInt holds the default value i.e 0

long

long data type is used to store 64-bit signed two’s complement integer. Its size is 8 byte. The value ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 both inclusive. It is used when dealing with bigger integral numbers that might not fit in the range of int.

Default Value: 0l

Example:

long myLong = 346464646;   // myLong holds the value 346464646
long negLong = -657656576; //negLong holds the value -657656576
long  defLong;             // defLong holds the default value i.e 0L

float

float data type is used to store 32-bit IEEE 754 floating values. Its size is 4 byte. The value ranges from 1.40239846 x 10-45  to 3.40282347 x 1038 both inclusive. It is used as a memory saving alternative to double(size 8 byte). High precision values are not recommended to be stored in float.

Default Value: 0.0f

Example:

float myFloat = 12324.56f; //myFloat holds value 12323.56

double

double data type is used to store 64-bit IEEE 754 floating values. Its size is 8 byte. The value ranges from 4.9406564584124654 x 10-324 to 1.7976931348623157 x 10308 both inclusive. It is the default choice for every programmer when dealing with decimal value unless the memory is to be concerned. High precision values are not recommended to be stored in double.

Default Value: 0.0d

Example:

double myDouble = 12324565.56; // myDouble holds value 12324565.56

Non-Primitive/Object/Reference Data types

Apart from primitive data types, we get the privilege to create our own data types. We can create our own class/data structure, suiting our requirement, and use it just like any other data type.

The variables are created using the constructors of objects. Class Objects and Array variables example of reference/object data types.

There are five Reference types:

  • Annotation
  • Array
  • Class
  • Enumeration
  • Array

There are many terms used here, which we are unaware of. You need not worry as this is just an overview and it will be elaborated in the upcoming classes in detail.

Default Value: null

This lecture was an overview of data types, in the next lecture we will look into the variable types.