What is the following code execution result?

Integer i1 = 10;
Integer i2 = 10;
           
Double d1 = 10d;
Double d2 = 10d;

System.out.println(i1 == i2);
System.out.println(d1 == d2);
Explanation
This is because of how java cache for auto-boxed integer values work. From Java Language Specification 5.1.7:

If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

And there is no such auto-boxing cache for Double class.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты