• Post Reply Bookmark Topic Watch Topic
  • New Topic

primitive float assignment  RSS feed

 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I though primitive float type assignment must with 'f' append ?

why is
float f1 = 10;         valid
but
float f1 = 10.1; not valid ?



Thanks.


 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10.1 consider as double, not float
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To use f1 like float, you need cast it, for example:

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketung Xiao wrote:
I though primitive float type assignment must with 'f' append ?

why is
float f1 = 10;         valid

I understand the question to be why is the assignment valid if the value does not have a f suffix.
 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the reply; double to float assignment would lost precision, hence not allowed.

I'm studying to prepare for Izo-808 cert exam.
This question is not hard; but I find it not easy to answer for checked vs unchecked exception.

Unless one has access to API for a given exception class name, how can tell whether a given exception is
subclass of Exception and would be a checked exception
or subclass of RunTimeException and would be a unchecked exception ?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketung Xiao wrote:
Thanks for the reply; double to float assignment would lost precision, hence not allowed.

Also loss of range - float has a significantly smaller range than double:

float: 3.40282347 x 10e38 ~ 1.40239846 x 10e-45
double: 1.7976931348623157 x 10e308 ~ 4.9406564584124654 x 10e-324
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
Ketung Xiao wrote:
Thanks for the reply; double to float assignment would lost precision, hence not allowed.

Also loss of range - float has a significantly smaller range than double:


Actually, lost precision is not the reason why implicit casting would not be allowed. For example, casting from long to float would also lose precision, but it is allowed... As Ron mentioned, the implicit casting rules is based on range.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!