Original link: http://www.cnblogs.com/lwbqqyumidi/p/3700164.html
There are 8 basic data types, namely Boolean Boolean, character char, and numeric type byte/short/int/long/float/double. Because the character char represents a single character corresponding to the corresponding shaping in the ASCII code, it is sometimes divided into numeric types. Reference types can be categorized into: arrays, classes, and interfaces. Therefore, the summary of the Java Type conversion in this article will also be divided into basic data types and reference data types two aspects of the expansion.
First, type conversions for basic data types
In the base data type, Boolean type Boolean occupies one byte, and because of its own special meaning of code, theBoolean type cannot be transformed with the other base type (neither an automatic type promotion nor a type cast), otherwise a compilation error occurs.
1. Automatic type promotion of numeric types in the base data type
Numeric types store their own values directly in memory, and in memory the corresponding size is allocated for different numeric types. For example, a variable of type Byte occupies 8 bits, an int type variable occupies 32 bits, and so on. Correspondingly, different numeric types will have a range of values that match their storage space. The following are the specific examples:
In turn, the number of bytes in each numeric type and the corresponding range of values are represented in the figure. In java, integer type (byte/short/int/long), the default type is int for non-declared data type. In floating-point type (float/double), for floating-point types that do not declare a data type, the default is double.
Let's take a look at one of the more classic examples below:
1 package com.corn.testcast; 2 3 4 public class Testcast {5 6 public static void Main (string[] args) {7 byte a = n; Compilation error Type mismatch:cannot convert from int to byte 8 float b = 1.5; Compilation error Type Mismatch:cannot convert from double to float 9 byte C = 3; Compiled correctly }11 12}
Isn't it a little strange? According to the above thinking to understand, an int type 1000 is assigned to a byte type of variable A, compile error, the hint "cannot convert from int to byte" is right, 1.5 default is a double type, It is also correct to assign a value of type double to a float type and compile an error. But the last sentence: to assign an int type 3 to a byte variable C, actually compiled correctly, this is why?
The reason for this is that when the JVM is compiled, when a numeric value of type int is defaulted to a numeric type variable that is smaller than the value range of type int (this unification is known as the value type K,k can be a byte/char/short type), it is judged if this int value exceeds the value type K , then a direct compilation error occurs. Because you assign a value that exceeds the range to a variable of type K, K does not fit, you have no coercion type conversion, of course, error. However, if the value of this int type is still within the value type K range, the JVM will customize an implicit type conversion to convert this int value to type K. The dashed arrows in the. This is a bit special and requires a little attention.
In other cases, when a type with a small range of numeric values is assigned to a numeric variable with a large range of values, the JVM automatically promotes the type of the numeric value during compilation. In the automatic type promotion of numeric types, numeric precision should at least not be reduced (integral types remain unchanged and float->double precision will be higher).
1 package com.corn.testcast; 2 3 public class Testcast {4 5 public static void Main (string[] args) {6 long a = 10000000000;//Compile Error: T He literal 10000000000 of type int is out of range 7 long B = 10000000000L;//compile correct 8 int c = n; 9 Long D = c;10 float e = 1.5f;11 double f = e;12 }13 14}
As above: When you define a variable of type long, you compile an error because 10000000000 defaults to the int type, and the value range of type int is -2^31 ~ 2^31-1, so 10000000000 has exceeded the maximum value in this range. Therefore, it has compiled the error itself, not to mention the assignment to the long variable A.
At this point, if you want to correctly assign a value, change the default type of 10000000000 itself, directly to 10000000000L can be defined as a long type. The assignment is now correctly compiled.
The int variable C with value 1000 is assigned to the long variable d, and as described above, an automatic type promotion occurs directly at this time, and the compilation is correct. Similarly, assigning E to F compiles correctly.
Next, there is one more place to note: The char type itself is unsigned type, and has two bytes, its value range is 0 ~ 2^16-1, because this directly causes the byte type cannot be automatically promoted to char, Char and short do not directly occur with automatic type promotion (because of negative numbers)and, of course, byte can be directly promoted to the short type.
2. Numeric type casts in the base data type
When we need to assign a numeric type with a large range of values to a numeric type variable with a smaller range of values, it is possible to lose precision at this point (except for the 1 implicit conversions from int to K type), so you need to convert manually. We call this coercion type conversion.
First, let's take a look at the following example:
1 package com.corn.testcast; 2 3 public class Testcast {4 5 public static void Main (string[] args) {6 byte p = 3;//compiled correctly: int to byte compiled An implicit type conversion occurred in 7 int a = 3; 8 byte B = A;//Compile Error: cannot convert from int to byte 9 byte c = (byte) A;//compile correctly float d = (float) 4.0;11 }12 13}
BYTE P = 3; compilation is correctly explained in 1. Next, a value of 3 int variable A is assigned to byte variable B, and a compilation error occurs. What is the difference between these two formulations?
The difference is that the former 3 is a direct amount, the compilation can be directly judged, the latter a is a variable, need to run period to determine, that is, during the compilation as a precaution, of course, it is impossible to compile the pass. At this point, a forced type conversion is required.
Forcing a type conversion can result in a loss of precision, and if the value is in the range of a small range of types, the precision of the integer variable is constant, but it is likely that there will be some surprises if it goes out of the range of a small type.
Here's a classic example:
Press CTRL + C to copy the code
Press CTRL + C to copy the code
Why is the result-23? Needs to be considered from the most fundamental binary storage.
233 binary is represented as: 24 bit 0 + 11101001,byte type only 8 bits, so from the high start to discard, after truncation left: 11101001, because the binary highest bit 1 is negative, 0 is a positive number, and its corresponding negative number is-23.
3. Automatic promotion of data types and possible forced type conversions when doing mathematical operations
The following code:
1 package com.corn.testcast; 2 3 public class Testcast {4 5 public static void Main (string[] args) {6 byte a = 3 + 5;//compile normal compile to 3+5 Changes directly to 8 7 int b = 3, c = 5; 8 byte d = b + C;//Compile Error: cannot convert from int to byte 9 byte E = ten, F = 11;1 1 Byte g = e + F;//Compilation error + direct 10 and 11 type promotion in order to int12 byte h = (byte) (E + f); Compile correctly }14 15}
When a mathematical operation is performed, the data type is automatically promoted to the larger of the operator, and so on. Forcing type conversions may be required when the result of the last operation is assigned to a specified numeric type.
Turn Java Type conversion