Data Types dictate the nature of values that can be stored or assigned in variables, methods, and functions during execution of Java code. There are mainly two broad data types in Java:
- Primitive Data Types: Primitive Data Types are fundamental data types that are frequently used to code anything in Java. The types are namely boolean, char, byte, short, int, long, float, and double,
- Non-Primitive Data Types – Non-Primitive Data Types include Classes, Interfaces, and Arrays.
Table of Contents
Primitive Data Types
As mentioned before, primitive data types are the building blocks of Java. These are fundamental data types that are universally used in all Java programs. There are 8 primitive data types:
- Boolean Data Type: Stores either ‘TRUE’ or ‘FALSE’.
Example: Boolean test = false;
- Byte Data Type: It is a 8-bit signed two’s complement integer with a range from -128 to 127. It has a default value of 0.
Example: byte test = 10;
- Short Data Type: It is a 16-bit signed two’s complement integer with a range from -32,768 to 32,767. Has a default value of 0.
Example: short test = 1000;
- Int Data Type: It is a 32-bit signed two’s complement integer with a default value of 0. Example: int a = 1,00,000;
- Long Data Type: 64-bit two’s complement integer.
Example: long test = 100L;
- Float Data Type: It is used for precision after decimal point.
Example: float f = 23.5f;
- Double Data Type: Used to deal with decimal numbers.
Example: double test = 20.3
- Char Data Type: Single 16-bit Unicode character.
Example: char ch = ‘A’;
Non-Primitive Data Types
These contain references to memory locations where the values are stored.
- String: String str = “Test Demo”;
- Class: User defined prototype.
- Object: Instances of a class and a basic unit of OOP.
- Interface: Similar to the structure of a class but with abstract methods.
- Array: Series of same-datatyped variables.