Java basics --- type conversion

Mainly divided into two kinds of invisible conversion and forced conversion.

① Implicit conversion:

The implicit conversion follows such a conversion chain:
byte, short, char—int—long—float—double
when invisible conversion is needed, it will be converted to the data type closer to the right side of the chain,
eg:

byte bb=2;
int cc=3;
byte dd=bb+cc;
int ee=bb+cc;

In the above code block, dd is wrong, ee is correct, because byte and int are converted into int when they are converted invisibly, and int is further to the right in the chain.

②Forced conversion:

Target type variable name = (target type) (data to be converted)
eg:

int a=2;
byte b=3;
byte c=(byte)(a+b);

Guess you like

Origin blog.csdn.net/Forest_2Cat/article/details/106619758