Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Integer type conversion in Java
Integer type conversion in Java
In Java, there are two types of type conversion: implicit type conversion and forced type conversion.

Implicit type conversion is the conversion from low-precision data to high-precision data, which is automatically performed by the system without manual implementation by programmers.

Forced type conversion is the conversion from high-precision data to low-precision data, which requires programmers to manually realize the conversion.

The order of precision from low to high is bytes.

Therefore, in the landlord's first question, the answer is that there is no need to force the conversion from byte short to int.

The question mentioned in the landlord's question involves the default type of Java, in which the integer type defaults to int.

Therefore, whether the a b defined by the landlord is byte or short, the result of a+b is int by default. The landlord uses byte or short variable to receive the operation result of A+B. Because the precision of int is higher than that of byte and short, it needs to be cast, otherwise the compilation will report an error.

//byte operation

Bytes? a = 1;

Bytes? b = 2;

Bytes? X= (bytes)? (a+b);

system . out . println(x);

//Short operation

Short? w = 1;

Short? s = 2;

Short? Z= (short)? (w+s);

system . out . println(z); Note: In this forced type conversion, because it involves the problem of type promotion in operation, is it a+b or w+s? The whole is enclosed in parentheses, otherwise the compiler will still report the error.