1. Automatic lifting type
herein discussed only seven kinds of operation data (does not include Boolean, Boolean because only two situations: false and true)
When a small capacity data type variable capacity type of data variables do arithmetic, the result is automatically promoted to a large capacity of data types
i.e., small capacity data type variable + capacity data type variable = large-capacity data type variable
byte → short → int → long → float → double
Note: in this case refers to the capacity size represents a range of large and small numbers.
For example: float capacity greater than the capacity of the long
In particular: when the byte, short, char three types of variables do calculation result to an int
class Test{
public static void main(String[] args) {
byte b1 = 2;
int i1 = 129;
//编译不通过
//byte b2 = b1 + i1;
int i2 = b1 + i1;
long l1 = b1 + i1;
System.out.println(i2);
float f = b1 + i1;
System.out.println(f);
short s1 = 123;
double d1 = s1;
System.out.println(d1);//123.0
//***************特别地*********************
char c1 = 'a';//97
int i3 = 10;
int i4 = c1 + i3;
System.out.println(i4);
short s2 = 10;
//char c2 = c1 + s2;//编译不通过
byte b2 = 10;
//char c3 = c1 + b2;//编译不通过
//short s3 = b2 + s2;//编译不通过
//short s4 = b1 + b2;//编译不通过
}
}
2. cast: automatic type lifting operation of an inverse operation
① to use strong turn symbol ()
② note, cast, may result in loss of precision
small-capacity variable type data → data type variable capacity, without losing original information.
The data type of the variable capacity → small-capacity data type variable, you may lose the original message.
class Test1 {
public static void main(String[] args) {
double d1 = 12.9;
//精度损失举例1
int i1 = (int)d1;//截断操作
System.out.println(i1);
//没有精度损失
long l1 = 123;
short s2 = (short)l1;
//精度损失举例2
int i2 = 128;
byte b = (byte)i2;
System.out.println(b);//-128
}
}