• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

long to float

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, as I was working with conversions, I noticed that you can convert without a cast from long to float. It's OK to convert from long to double as they both are 64bits but why can I convert from long to float:

thanks
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because somebody made a decision to permit that back in the early history of the language. You can see which conversions are permissible in the Java® Language Specification. After 20+ years, it may be difficult to get more details than what I told you.

I trust you don't plan ever to use floats in real life, unless for this, or other API requiring them.
 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implicit conversion is based on range only. The maximum value for a float  (not counting infinity) is greater than the maximum value for a long. And the minimum value for a float  (not counting negative infinity) is less than the minimum value for a long.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because somebody made a decision to permit that back in the early history of the language.
ahaha, I love that, but fair enough.
Yes, correct, I will probably never need it for real life programming, but I might for the OCA java 8 exam. i honestly don't remember if I asked it in another thread -apologies if I did - but is implicit conversion on the exam? A quick glance at the exam's objective doesn't seem to have it on http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=5001&get_params=p_exam_id:1Z0-808
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a bad idea to learn things (or not) depending on whether they are in the exam. You learn things because they are interesting, or because you think they will be useful to know in real life.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic