Default conversion and forced conversion of data types in Java, java Data Type
Default conversion:
A: from low to high :(Byte short char)---Int---Long---Float---Double
B: Note:Byte short charThey do not convert each other. They are involved in the calculation and are first converted to the int type.
Forced type conversion:
Format: Target Data Type Variable = (target data type) (converted data)
Among them, string data is good for other data+The result is of the string type,+Here is the connector
Classic example:
1. JudgmentShort s1 = 1; s1 = s1 + 1; AndShort s1 = 1; s1 + = 1; Difference
Resolution:
Short s1 = 1; s1 = s1 + 1; Format Error
BecauseS1 + = 1The expression type is automatically upgraded during operation. The result isIntType, and then assignedShortType, the error of forced type conversion is reported.
Short s1 = 1; s1 + = 1; The format is correct.
BecauseS1 + = 1; Is the specified operator and the encoder performs special processing;
2,Float f= 3.4Correct?
Resolution:
Error
If the precision is not accurate, forced type conversion should be used, for example:Float f = (flat) 3.4OrFloat f = 3.4 f;
In Java, the default value without a decimal point isIntType. The default value with a decimal point isDoubleType
Both questions are Java interview questions, which are very basic and important.