Java implicit conversion

Use the following code:

Float a = 1.2;

There is an error because it takes decimal as the value of double, which is a larger data type than float

It now takes integers as the default int type So why doesn't the following code give any errors?

Byte b = 20;

Solution

The compiler is smart enough to make sure that the 20 bit representation (int value) can fit a byte without losing data From Java language specification § 5.1 3:

See also this thread

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>