Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing long and double

 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does this return true.
Doesn't long and double have different bit patterns?
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparision of primitives only compares values
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, the long value will be promoted to a double value before the equality expression is evaluated. Thus, the values are deemed to be equal. The widening rules are as follows:

1. If either expression is of type double, the other is converted to a double
2. If either expression is of type float, the other is converted to a float
3. If either exprssion is of type long, the other is converted to a long
4. Otherwise, both operands are converted to int
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic