1. First review the basic Java data types, see
2. Compare their number of bytes
Note: 1 bytes (byte) = 8 bits (bit)
3. Knowledge points in the conversion
The default int type for integer types in Java, and the default double for decimal types;
Char can be used as a special integer type;
int cannot be converted to Boolean;
Decimal type to integer type, decimal may be discarded, all occurrences of loss of precision, so need to cast;
The Boolean type cannot be converted to any other data type;
byte B2 = 120;
The reason for no error:
At compile time, check to see if the size of the assignment exceeds the range that the variable type holds
If more than, error: conversion from int to byte may have a loss, if not exceeded, compiled by
float F3 = 100L; In this case, the integer part can be directly assigned to the float integer part;
float f1 = 100.9; This situation, because the default is double, if this conversion, it is possible to lose the decimal point, must be cast;
Long L3 = 1000.9f; Decimal to Integer, decimal may be lost, need to cast;
Double D2 = 10.9d;
int i2 = D2; Error: Incompatible type: conversion from double to int can be a loss;
Char C1 = ' a ';
int i3 = C1; Automatic conversion
int i4 = 100;
Char C2 = i4;//Error: Incompatible type: conversion from int to Char can be a loss;
4. In arithmetic
/*
1, if one of the two operands is a double type, the other is converted to a double type;
2, otherwise, if one of the operands is a float, the other will be converted to float;
3, otherwise, if there is one operand is long, the other will be converted to long;
4, otherwise, two operands will be converted to the INT type.
*/
Interview Traps
BYTE B1 = 10;
byte B2 = 11;
Error: Incompatible type: conversion from int to byte could be a loss
Otherwise, the two operands are converted to the int type.
BYTE B3 = b1 + b2//Error
byte B3 = (byte) (B1 + b2); That's right
Interview Trap 2:
Short S1 = 1;
S1 = s1 + 1; Error: Incompatible type: conversion from int to short may be a loss
Short S2 = 1;
S2 + = 1; Equivalent to Short b3 = (short) (S2 + (short) 1); That's right
5. The last is a small to large sequence of pictures
Java basic data types and conversions between them