First look at the basic types in Java, such as 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 length are fixed, not because of the different machines, so that Java has a good portability.
When you are doing a project recently, you need to store a 16-bit long binary number in a variable to see if the short type fits your needs.
But because the number types in Java are signed, the first bit of the short type is used to represent the symbol, and the actual storage length is only 15 bits.
That is -7fff-+7fff. What if we want to store +7FFF-+FFFF's numbers? Only a workaround is adopted, using a transform algorithm. We can use -7fff--0001 to store numbers larger than 7FFF. The conversion formula is-(ffff-x)-1. Because of the symbol, so +0000 and 0000 are equal, so less than the unsigned number of 1 numbers, so to subtract 1 in the formula, because of this problem, we can not store +8000 with this method.
This method can basically meet our needs, if there is a special need, such as to store +8000, that only to take another approach.
Short type variables in Java