First, let's take a look at the basic types in Java, as shown in the following table:
|
Primitive type |
Size |
Minimum |
Maximum |
Wrapper type |
|
Boolean |
- |
- |
- |
Boolean |
|
Char |
16-bit |
Unicode 0 |
Unicode 216-1 |
Character |
|
Byte |
8-bit |
-128 |
+ 127 |
Byte |
|
Short |
16-bit |
-215 |
+ 215-1 |
Short |
|
Int |
32-bit |
-231 |
+ 231-1 |
Integer |
|
Long |
64-bit |
-263 |
+ 263-1 |
Long |
|
Float |
32-bit |
Ieee754 |
Ieee754 |
Float |
|
Double |
64-bit |
Ieee754 |
Ieee754 |
Double |
|
Void |
- |
- |
- |
Void |
Java's basic types of storage have a fixed length, which does not vary with machines. Therefore, Java has good portability.
Recently, when working on a project, you need to store 16-bit long binary numbers in a variable. The above table shows that the short type meets the requirements.
However, because the numeric types in Java are signed, the first part of the short type is used to represent the symbol. The actual storage length is only 15 characters,
That is,-7fff-+ 7fff. What should I do if I want to store + 7fff-+ FFFF numbers? Only a work und is adopted, and the transformation algorithm is used. We can use-7fff--0001 to store numbers greater than 7fff. The conversion formula is-(FFFF-X)-1. Because of the symbols, + 0000 and-0000 are equal. Therefore, one digit is less than the number of unsigned characters, so 1 is required in the formula. Due to this problem, with this method, we cannot store more than 8000.
Using this method can basically meet our needs. If there are special needs, for example, to store more than 8000, we only need to adopt other methods.