Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
• Primitive Data Types
• Non-primitive data types
Primitive Data Types
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
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.
• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128
• Maximum value is 127
• 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
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768
• Maximum value is 32,767
• Default value is 0
Integer is generally used as the default data type for integral values unless there is a concern about memory.
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648
• Maximum value is 2,147,483,647
• The default value is 0
This type is used when a wider range than int is needed
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808
• Maximum value is 9,223,372,036,854,775,807
• Default value is 0L
Float data type is never used for precise values such as currency.
• Float data type is a single-precision 32-bit IEEE 754 floating point
• Default value is 0.0f
This data type is generally used as the default data type for decimal values, generally the default choice
• double data type is a double-precision 64-bit IEEE 754 floating point
• Double data type should never be used for precise values such as currency
• Default value is 0.0d
This data type is used for simple flags that track true/false conditions.
• boolean data type represents one bit of information
• There are only two possible values: true and false
• Default value is false
Char data type is used to store any character.
• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.