When we assign integer to int in Java, why doesn’t the compiler make an error

When we assign integer (object) to int (primitive), why does the compiler not make an error?

int i;
Integer ii = new Integer(5);
i = ii;//no compilation error.

This is the case for all other types (byte byte, float float)

Why? Did I miss anything?

Solution

It is called auto@R_433_2419 @ing / un@R_433_2419 @ing.

Starting from Java 1.5, the compiler will automatically "box" primitives into corresponding classes (such as int and integer, double and double, etc.), and cancel the box as needed

For details, see this page in the documentation

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
分享
二维码
< <上一篇
下一篇>>