The Java language is a static type (statical typed), which means that all variables and expression types are fully determined when they are recompiled. Because it is a static type, the Java language is also strongly typed (strong typed). Strong typing means that each variable has a type, each expression has one type, and each type is strictly defined, and the type restricts what values the variable can receive and what value the expression ultimately produces. It also limits the types of operations these values can take and how they are manipulated. All assignment operations, whether explicit or passed by parameter in a method call, are checked for type compatibility.
Data type
In the Java source code, each variable must declare a type. There are two types: the base type (primitive type) and the reference type (reference type). The reference type refers to the object (reference to object), and the base type directly contains the value (directly contain value). The base type (primitive types) includes a Boolean type and a numeric type (numeric types). Numeric types are also classified as Integer (integer types) and floating-point (floating-point type). There are 5 types of integers: byte short int long char (char is essentially a special int). Floating-point types have float and double. The relationship is organized like this:
Null is a special type, but you cannot declare a variable to be a null type, and the only value of a null type is NULL. Null can be negative to any reference type or to any reference type. In practice, NULL is generally used as the literal value (literal), which is a literal reference type.
Other reference types are described later and are not covered here.
Basic type
The base type is a single value, not a complex object, and the base type is not object-oriented, primarily out of efficiency considerations, but also provides the basic type of object version, the basic type of wrapper (wrapper). You can use these basic types directly, or you can construct arrays or other custom types using the base type. The base type has a definite range of values and mathematical behavior.
| Basic type |
Size |
Minimum value |
Maximum Value |
Packing class |
| Boolean |
-- |
-- |
-- |
Boolean |
| Char |
+ Bits |
Unicode 0 |
Unicode 2-1 |
Character |
| Byte |
8 bits |
-128 |
+127 |
Byte |
| Short |
+ Bits |
-2^15 |
+2^15-1 |
Short |
| Int |
Bits |
-2^31 |
+2^31-1 |
Integer |
| Long |
+ Bits |
-2^63 |
+2^63-1 |
Long |
| Float |
Bits |
IEEE754 |
IEEE754 |
Float |
| Double |
+ Bits |
IEEE754 |
IEEE754 |
Double |
| void |
-- |
-- |
-- |
Void |
Conversions between basic types
It is common to assign a value of one type to another type. In Java, aBoolean type cannot be converted from all other 7 types . Other 7 types of numeric values can be converted to each other, but there may be a loss of precision or some other change. Conversions are divided into automatic conversions and casts. For automatic conversions (implicit), no action is required, and coercion of type conversions requires an explicit conversion, that is, the conversion operator is used.
7 basic types of conversions are summarized as:
You can assign an int to a byte short char int in the process of assigning a value to an integer using literal values, as long as the range is not exceeded. This process is done automatically when the type is converted, but if you try to assign a long to byte, you must also force the type conversion, even if you are not out of range. For example, byte B = 10L; it is wrong to make a cast.
In addition to assignment, some type conversions can occur during expression evaluation. In an expression, the type promotion rule is as follows:
- All Byte/short/char are promoted to int
- If there is an operand of long, the entire expression is promoted to long. The same is true for float and double.
Java Basic data types