Primitive Data Types
Let's start by diving into the primitive data types that Java offers.
The primitive data types include byte
, short
, int
, long
, float
, double
, boolean
, and char
. The primitive data type specifies the size and type of variable values.
byte
A Byte data type is used to save space in larger arrays in the place of integers. A byte is four times smaller than an integer. The default value of a byte is 0.
byte b = 90 ;
short
A short data type can be used to save memory similar to a byte data type. A short is two times smaller than an integer. The default value of a short is 0.
short s = 1200;
int
An integer is the conventionally used default data type for values unless there is a concern about memory. The default value of an integer is 0.
int var = 5;
long
A long is a type used when a wider range than an int is needed. The default value of a long is 0L.
long l = 1000l;
float
A float is mainly used to save memory in large data structures of floating point numbers or to represent a collection of floating point numbers. The default value of a float is 0.0f.
float f = 3.14f
boolean
A boolean data type is used for flags that examine true or false conditions. There are only two possible values: true and false. The default value is false.
boolean var = true;
double
A double is generally used as the default data type for decimal values. It can be used to represent both floating point and decimal numbers. The default value is 0.0d.
double d = 1000.2;
char
A char is used to store any character.
char ch= 'A';