Special cases of type conversion in Java

xiaoxiao2021-03-06  62

Let's take a look at the following statements are illegal.

BYTE B1 = 2;

BYTE B2 = 1 1;

BYTE B3 = B1 1;

BYTE B4 = B1 ;

INT I1 = integer.max_value 1; //integer.max_value=2147483647

INT I2 = 2147483648;

Change Byte to CHAR and SHORT almost the same.

Because the integer graphic constant is int type, that is, 1 and 2 are int types.

In the assignment statement, an int model is definitely can't be ignored.

INT i = 1;

BYTE B = I;

It is compiled because the implicit type conversion is not allowed, and it can be changed below.

INT i = 1;

BYTE B = (Byte) i;

Recessive conversion is generally relaxed conversion (such as Byte to int, string to Object)

But there is an exception that is in the assignment conversion,

The implicit conversion also includes narrowing basic conversions.

That is, from int to byte, char, short conversion:

such as:

BYTE B = 1; // Legal

BYTE B = 128; // illegal

The right side must be int models, and the value must be indicated by the left type.

转载请注明原文地址:https://www.9cbs.com/read-118982.html

New Post(0)