Java automatic type conversion and casts

Data type to be converted into an implicit (automatic type conversions) and explicit conversions (casts) two.

Implicit conversion (automatic conversion)

When the two conditions are satisfied, then the type of data is assigned to another type of variable, the type of conversion to be performed automatically (automatic type conversion).

	两种数据类型彼此兼容
	
	目标类型的取值范围大于源数据类型(低级类型数据转换成高级类型数据)

When the above two conditions are satisfied, automatic conversion (widening conversion) occurs. For example, to convert a byte type short, due to the large type of short range, it will automatically be converted to byte short type.

In the course of operation, due to the different types of data is converted into the same data type, integer, floating point and the character can participate in mixed operation. Automatic conversion rule is converted into a low-level type of data from the high-level data types . Conversion rules are as follows:

	数值型数据的转换:byte→short→int→long→float→double。
	
	字符型转换为整型:char→int。

The above data type to follow the conversion sequence from left to right, is converted into the final expression indicates the data type of the variable range of the maximum.

Examples of automatic type conversion
customers shopping in a supermarket to buy toothpaste 2 boxes, 4 boxes of Kleenex. In which the price is 10.9 yuan toothpaste, Kleenex price is 5.8 yuan, the total price of commodity demand.

achieve

public static void main(String[] args) {
    float price1 = 10.9f; // 定义牙膏的价格
    double price2 = 5.8; // 定义面巾纸的价格
    int num1 = 2; // 定义牙膏的数量
    int num2 = 4; // 定义面巾纸的数量
    double res = price1 * num1 + price2 * num2; // 计算总价
    System.out.println("一共付给收银员" + res + "元"); // 输出总价
}

The above code first defines a price variable storage toothpaste float type, then define the price of a double Kleenex variable storage type, and then define the number of two variables of type int storing items, and finally the multiplication operation and Thereafter, the output result is stored in a variable of type double.

Output

一共付给收银员44.999923706055

Seen from the execution result, float, int, and double three types of data involved in operation, the result is the final output data of type double. This conversion is generally referred to as "an expression of the type of automatic upgrade."

Casts using a display, for example:

byte b = 50;
b = (byte)(b*2);

This will produce the correct value of 100.

note: Char special type, char automatically converted int, long, float, and double, but not automatically convert byte, and short of char, but can not be automatically converted to char byte or short.

Explicit conversion (cast)

Although automatic type conversion is helpful, but does not meet all of the programming needs. For example, if you need a double type value to a variable of type int, how would you do?

This conversion does not happen automatically, because of changes in the scope of double type is smaller than an int. This conversion has to make a "narrowing conversion" because you certainly want to source data type value becomes smaller in order to fit the target data type.

When the two data types are not compatible, or in the range less than the target type of source type, it can not be converted automatically, then they need to be cast . The syntax is as follows:

(type)variableName

Here, type variableName is to be converted into the data type for the variable name is simply more variableName type conversion, examples of cast follows:

int a = 3;
double b = 9.0;
a = (int)b;

The above-described first code type double the value of variable b cast to type int, then assign the value of a, b-value but variable itself is not changed.

In the cast, the floating-point type if the value is converted to an integer, a decimal point directly remove all behind; and if it is an integer of casts of floating type, the zero padding after the decimal point.

Examples of casts
Customers shopping in a supermarket to buy toothpaste 2 boxes, 4 boxes of Kleenex. Wherein the toothpaste is 10.9 yuan price, the price of 5.8 yuan facial tissues, find the total price of goods, using an int data stored in the calculation of the total price. Codes are as follows:

public static void main(String[] args) {
    float price1 = 10.9f;
    double price2 = 5.8;
    int num1 = 2;
    int num2 = 4;
    int res2 = (int) (price1 * num1 + price2 * num2);
    System.out.println("一共付给收银员" + res2 + "元");
}

In the above example, there are double type, float type, and data type int involved in computing, the calculation results Default type double, the result of type int requirements of the subject, because an int in the range smaller than the range of type double , so the need for mandatory conversion.

Output

一共付给收银员44
Published 383 original articles · won praise 92 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104636001