What are Data Types in Java?
Data types specify the varying sizes and values in the variables that can be stored. That is every variable is assigned by data-types according to the need. And based on their respective data-types, the operating system allocates memory to that data-types.
There are two data types in Java:
- Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
- Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
1. Primitive data types:
These are the most basic data types available in the Java language.
There are eight primitive data-types available by Java:
|byte||1 byte||Stores whole numbers from -128 to 127|
|short||2 bytes||Stores whole numbers from -128 to 127|
|char||2 bytes||Stores whole numbers from -32,768 to 32,767|
|int||4 bytes||Stores a single character/letter or ASCII values|
|long||8 bytes||Stores whole numbers from -2,147,483,648 to 2,147,483,647|
|float||4 bytes||Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|double||8 bytes||Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits|
|boolean||1 bit||Stores true or false values|
These are put into four group:
- Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
- Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
- Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
- Boolean: This group includes boolean, which is a special type for representing true/false values.
It is an 8-bit signed two’s complement integer. The byte data type is used to save memory in large arrays.
Its minimum value is -128 and the maximum value is 127. Its default value is 0.
byte x = 15;
byte y = -12;
The short data type is a 16-bit signed two’s complement integer. A short is 2 times smaller than an integer. Its minimum value is -32,768 and the maximum value is 32,767. Its default value is 0. And is also used to save memory.
short x = 1500;
short y = -1200;
The int data type is a 32-bit signed two’s complement integer. It is a default data-type for integral values.Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0.
int x = 25;
int y = -35;
The long data type is a 64-bit two’s complement integer. Its value-range lies 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. long is used when int is not large enough to store the value.
x = 200000L;
long y = -300000L;
Floating Point Types:
The float data type is a single-precision 32-bit IEEE 754 floating-point. It is used when the user needs to save memory in large arrays of floating-point numbers. float is never used for precise values such as for currency. Its default value is 0.0d.
float x = 234.5f;
The double data type is a double-precision 64-bit IEEE 754 floating-point. The double is generally used for decimal values just like float. The double also should never be used for precise values, such as currency. Its default value is 0.0d.
double x = 13.99d;
The char data type is a single 16-bit Unicode character. The size of a character is 2 bytes. It is used to store single characters and must be enclosed within single quotes as shown in the example. Example:
char alphabet = 'A';
A boolean keyword is used to declare with the boolean data-type and represents one bit of information that is either true or false.
boolean fun = true;
boolean bored = false;
2. Non-Primitive Data Types:
The non-primitive data types include Strings, Classes, Interfaces, and Arrays. These are also known as reference types because they refer to objects.
Non-primitive types are created by the user/programmer and are not predefined by Java except one that is String and can have null values whereas primitive data types are predefined in Java and always has a value.
To learn about Non-Primitive Data Types click on the following:
Strings, Classes, Interfaces, Arrays.