Pictured courseware theme
according to the views of different numerical variable calculation means when the small-capacity data types will automatically switch to high-capacity data type, data type capacity as shown below:
According to the above interpretation is understood as a personal byte
type capacity of 1 byte short
2 bytes the two data types when the result of the operation should be short
the type of data
error when it runs the code below
package com.company;
public class Main {
public static void main(String[] args) {
byte b = 1;
short s = 12;
short s2 = b + s;
System.out.println(s2);
}
}
abnormal:
Reason: The results of numerical calculation of the default data type is int
the type of data, when it is not declared short s2
Operation rules for number types:
- 1. When there are multiple types of data mixed operations, the system first automatically converts all data into the data type with the largest capacity, and then performs calculations. The number types from small to large are byte, short, int, long, float, and double.
- 2. In the calculation of numeric type, when multiple variables of the same type participate in the operation, the variable must first be converted to the default type of the corresponding data type (for example, if two variables of byte type are added, two variables of byte type will be added first. After converting to the default int type and then calculating, the result is int type). This situation applies to the capacity of the data type of the variable is smaller than the capacity of the default type, (for example, byte, short, all smaller than int)
- 3. There is no mutual conversion between byte, short, and char. The three of them are first converted to int type during calculation.