All data types are listed here first:
(1) Integral type: Byte,short,int, long
(2) floating-point type: float,double
(3) Character type: Char
(4) String type: string (Note that string is not part of the base data type)
(5) Boolean type: Boolean
The above finishing has been arranged according to the accuracy of the high and low (if the data type itself is accurate)
First, it should be understood that in the same type, low precision can be directly converted to high-precision data types, such as the byte data type can be directly used to accept the short data type, in the same vein, the short data type can be directly accepted by the INT data type. High-precision conversion to low-precision can be achieved by strong-turn. , so the following only discusses conversions between different types of data, as well as special cases.
- Between integral types:
A) The operation between Byte,short is automatically converted to the int type, the instance
It turns out that even calculations between the same data types are automatically converted to int
b) Special case: the magic of + = and-=
As we all know, the definition of the variable short s=5; s=s+5 the same as s+=5; but in fact, s=s+5; error, this is the same as above, short,byte operation with automatic conversion, The result of the operation will be converted from short to int (in fact Char is also converted to int when participating in the operation), but s+=5;
Brainstorming: Does the designer worry about an overflow when the operation is being generated?
- Floating point Type
A) float and double are automatically converted to double (low precision to high precision automatic conversion)
b) float and double are automatically converted to double (low precision to high precision automatic conversion), which can be introduced
I. Float in the case of data types that are less accurate than himself (all integer data: Byte,short,int,long, Integer constants), the result is a type of float
II. Double with all integer data types and float types, the result is double. Principle Ibid.
c) Loss of precision when converting a float or double to int,short,byte, he will omit the digits after the decimal point directly, and will not be rounded.
D) a strange question,
The arrow refers to a number less than or equal to 5,double output after a decimal point of 16 bits
The arrow refers to a number greater than or equal to 5,double output after a decimal point of 15 bits
The double type has the potential for loss of precision, so you should try to avoid using double types of data in your project.
- Character type
A) When converting a character type to an integer type, it is converted to his ASCII code value.
b) Number of character types if you want to convert to an integer type to participate in the calculation (the actual number represented by the character), you can subtract ' 0 '.
Principle: The ASCII code corresponding to the number is coherent
c) The principle of converting characters to floating-point is not repeating, character-int-floating point.
- String can only be converted to a character array, using the ToCharArray () method
You can also use the Charat () method
- There are two ways to convert Int to string:
About type conversions in Java