In Java programs, the values of different primitive types often need to be converted between different types. Java provides 7 numeric types that can be converted to one another, with two types of conversions: automatic type conversion and forced type conversion.
"Automatic type conversion
Assigning a value of a primitive type to a variable of another primitive type is called an automatic type conversion.
When a numeric value or variable with a small range of tables is directly assigned to a variable with a large range of table numbers, the system can automatically type conversions.
The value type on the right of the arrow can be converted automatically to the numeric type of the arrow to the right, on the example:
Not only that, when you concatenate a base type value with a string value, the base type value is automatically converted to a string type, although the string type is no longer the base type, but the reference type. Therefore, if you want to convert the base type to the corresponding string, you can concatenate the value of the base type with an empty string.
"Coercion type conversion
To convert the type of the arrow to the left of the arrow, you need to force the type conversion, forcing the type conversion syntax: (variable type) value, when the forced type conversion, it is equivalent to the large bottle of water into a small bottle, the water inside the large bottle is less than the water in the small bottle when it is OK, If more time, it is easy to overflow, resulting in data loss.
When you cast 233 to a byte-type integer above, it becomes-23, which is a typical overflow.
Conversion process:
From the 32-bit int cast to 8-bit byte-type process, to intercept the previous 24 bits, only the next 8 bits, where the leftmost is a sign bit, here is a negative, negative in the computer is in the form of complement, so also need to convert into source code.
java--type conversions for basic types