Java Data types
Java is a strongly typed language, so you must declare a type for each variable.
There are 8 basic types of Java, including: integer 4, floating-point 2, Unicode character 1, and Boolean-value type 1 .
Integral type
Java integral type
| Type |
Storage space |
Range of values |
Int |
4 bytes |
-2147483648 ~ 2147483647 |
| Short |
2 bytes |
-32768 ~ 32767 |
| Long |
8 bytes |
-9223372036854775808 ~ 9223372036854775807 |
| Byte |
1 bytes |
-128 ~ 127 |
Floating-point type: Used to represent a numeric value with a decimal part.
Java floating-point type
| type |
storage space |
value range |
| float |
4 bytes |
approximately ±3.40282347e + 38F (number of significant digits is 6~7 bit) |
| double |
8 bytes |
approximately ±1.79769313486231570e + 308 (significant digits 15 bits) |
A value of type float has a suffix f, (for example, 3.14F), and if a floating-point value without an F-suffix defaults to a double type, a double-type floating-point value can be appended with a suffix D, such as 3.14D.
Floating-point values do not apply to financial calculations with incoming errors, for example: 2.0-1.1 will appear = 0.89999999999999 instead of 0.9, which is mainly due to the fact that floating-point values are represented by a binary system, while in binary systems it is not possible to accurately represent a fraction of 1/ 10, it is as if the decimal does not accurately represent 1/3. If you need to have no rounding errors in the numerical calculations, you should use the BigDecimal class.
Char type
The char type is used to represent a single character, and its Unicode encoding unit can be represented as a hexadecimal value ranging from \u0000 to \UFFFF. A char type is a deprecated numeric type, preferably a string that needs to be processed in an abstract data type.
Boolean type (Boolean type)
A Boolean (Boolean) type has only two values: True and False. These two values are used to determine whether the logic is true or false. An integer value and a Boolean value cannot be converted to each other in Java.
Java Data types