Data Types in Java
Java data types are used to define the type of data for the different types of variables, constants, method parameters, returns type, etc. The data type tells the compiler about the type of data to be stored and the required memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Types of Data Types in Java
There are two data types available 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 Type include Classes, Interface and Arrays.
Java Primitive Data Types
Primitive data types are predefined by the language and named by a keyword. There are eight primitive data types supported by Java. Below is the list of the primitive data types:
- byte
- short
- int
- long
- float
- double
- Boolean
| Data Type | Default Value | Default size |
|---|---|---|
| boolean | false | 1 bit |
| char | ‘\u0000’ | 2 byte |
| byte | 0 | 1 byte |
| short | 0 | 2 byte |
| int | 0 | 4 byte |
| long | 0L | 8 byte |
| float | 0.0f | 4 byte |
| double | 0.0d | 8 byte |
Boolean Data Types:
when you will need to store data in true or false value, You can use Boolean data type.
This data type is used for simple flags that track true / false conditions.
Example: boolean flag = true;
Byte Data Types:
Byte data type is an 8-bit signed two’s complement integer. It’s minimum value is -128 and maximum value is 127. Its default value is 0.
Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
Example: byte a = 10;
Short Data Types:
Short data type is a 16-bit signed two’s complement integer. It’s minimum value is -32768 and maximum value is 32767. Its default value is 0.
Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer.
Example: short a = 10000;
Int Data Types:
Int data type is a 32-bit signed two’s complement integer.Its minimum value is – 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.
The int data type is generally used as a default data type for integral values unless if there is no problem about memory.
Example: int a = 100;
Long Data Types:
Long data type is a 64-bit signed two’s complement integer. Its minimum value is -9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default value is 0.
Example: long a = 10000L;
Float Data Types:
Float data type is a single-precision 32-bit IEEE 754 floating point. Float is mainly used to save memory in large arrays of floating point numbers. Default value is 0.0f.Float data type is never used for precise values such as currency.
Example: float a = 10.3f;
Double Data Types:
double data type is a double-precision 64-bit IEEE 754 floating point. This data type is generally used as the default data type for decimal values, generally the default choice. Double data type should never be used for precise values such as currency. Default value is 0.0d.
Example: double d = 53.6;
Char Data Types:
char data type is a single 16-bit Unicode character. Stores a single character/letter or ASCII values.