Introduction to Data Types in Java

Here, we will discuss the data types in Java. There are two types of data types- Primitive and Non-Primitive data types.
Table of Contents
Data Types in Java

Data Types in Java

Data types are defined as attributes which is used to specify the different values and sizes which can be stored in a variable.

Types of Data types in Java

The Java Data types are divided into two further categories i.e.,

- Primitive Data Types

- Non-Primitive Data Types

Now, let's discuss about these Data types in detail;

1. Primitive Data Types in Java

Primitive data types are the most simplest data type in Java which are defined as building blocks of data manipulation. 

The primitive data types are further divided into 8 types which are as follows;

• Boolean Data type

The data type which is used to store only two values i.e. true and false is known as Boolean Data type. Boolean are used for simple flags in order to track the condition of true or false.

boolean isJavaFun = true;

boolean isFishTasty = false;    

System.out.println(isJavaFun);

System.out.println(isFishTasty);

• Char Data type 

Char data type is a single 16-bit unicode which is used to store a single character and its values lies between '\u0000' to '\uffff'.

 char myGrade = 'B';

System.out.println(myGrade);

• Byte Data type

The primitive data type which is used to store whole numbers is known as Byte data type. And its value range lies between -128 to 127 and 0 is its default value. Byte data type is used in place of 'int' in order to save memory.

 byte myNum = 100;

System.out.println(myNum);

• Int Data type

Int is a data type which is used to store whole numbers which lies between -2147483648 to 2147483647 and 0 is its default value.

 int myNum = 100000;

System.out.println(myNum);

• Long Data type

Long data type is used when the value is large and 'int' is not able to store value. The value range of Long Data type lies between -9223372036854775808 to 9223372036854775807.

 long myNum = 15000000000L;

System.out.println(myNum);

• Short Data type

Short Data type is a primitive data type which is used to store whole numbers which lies between -32768 to 32767. Short Data type is 2 times smaller in size than 'int'.

 short myNum = 5000;

System.out.println(myNum);

• Float Data type

Float data type are data types which are used to store fractional numbers. Its value lies between 3.4e−038 to 3.4e+038. And 0.0F is its default value.

 float myNum = 5.75f;

System.out.println(myNum);

• Double Data type

Double data type are also used to store fractional numbers whose values lies between1.7e−308 to 1.7e+308 and 0.0d is its default value.

 double myNum = 19.99d;

System.out.println(myNum);

2. Non-Primitive Data Types in Java

Non-Primitive data types refer to objects which is why they are known as reference types. Classes, arrays and interfaces are included in Non-Primitive data type.