The following code compiles fine and yields true in java. I have read that java won't do two conversions at once, like when assigning an int literal value(or variable) to a Double wrapper reference. So why is it that this compiles fine in comparison with using = operator?
double double1 = 3.00;
Integer wInt = new Integer("3");
if(wInt == double1);
Like other mathematical operators such as +
, the ==
operator performs binary numeric promotion on its operands.
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
Widening primitive conversion (§5.1.2) is applied to convert either or both operands...
The compiler first unboxes the Integer
to an int
, then widens the int
to a double
. It will perform both if the unboxing occurs first.
Java will perform both conversions implicitly for many operators:
Binary numeric promotion is performed on the operands of certain operators:
The multiplicative operators
*
,/
, and%
(§15.17)The addition and subtraction operators for numeric types
+
and-
(§15.18.2)The numerical comparison operators
<
,<=
,>
, and>=
(§15.20.1)The numerical equality operators
==
and!=
(§15.21.1)The integer bitwise operators
&
,^
, and|
(§15.22.1)In certain cases, the conditional operator
? :
(§15.25)