aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Long and float comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Long and float comparison" Watch "Long and float comparison" New topic
Author

Long and float comparison

Justyna Wozniak
Greenhorn

Joined: Oct 04, 2006
Posts: 14
Hello,
this question is based on K&B, page 302, Q3.
Why does the following piece of code compile:

?
Does x get unboxed to a long and then converted to a float?
Or is x.floatValue() called?

Thanks,
Justyna W.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Justyna Wozniak:
Hello,
this question is based on K&B, page 302, Q3.
Why does the following piece of code compile:

?
Does x get unboxed to a long and then converted to a float?
Or is x.floatValue() called?

Thanks,
Justyna W.


42L is boxed to a Long, but in the comparison it is autounboxed.
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by Keith Lynn:


42L is boxed to a Long, but in the comparison it is autounboxed.


So it's technically 'unboxed' and then 'widened' (widened to make the comparison to a float)?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Yes, it is converted back to a long, and then the normal rules of binary promotion will promote it to a float since the other operand is a float.

For reference, section 5.6.2 of the Java Language Specification defines binary numeric promotion.

5.6.2 Binary Numeric Promotion
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, using widening conversion (�5.1.2) to convert operands as necessary:

* If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (�5.1.13) is applied to each operand.

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Long and float comparison
 
Similar Threads
operator flow
operator
Comparison of arrays
HI
Compiling errata for K&B, SCJP 6