This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Implicity Casting in Java

 
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would this fail to compile?



My thinking is that the compiler "must know" that 10 fits into float and d wont take another value because it is final.

where as this compiles?

 
Marshal
Posts: 64998
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch That is quite a difficult question.

As you know, “implicitly casting” means a widening conversion carried out automatically. This Java® Language Specification (=JLS) section tells you about widening conversions, and double→float isn't in there. You will find it in this JLS section about narrowing conversions. But it doesn't tell you whether it can compile. You need this section about assignments.

That JLS section wrote:In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

•   A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.

•   A narrowing primitive conversion followed by a boxing conversion may be used if the variable is of type Byte, Short, or Character, and the value of the constant expression is representable in the type byte, short, or char respectively.

The type of your first constant expression isn't byte, short, char, or int; It is double, which means it doesn't fall in the scope of that section. So you cannot compile the assignment of a double to a float without an explicit (float) cast.
 
Nation Chirara
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ritchie for the informative answer, now I got it all.
 
Campbell Ritchie
Marshal
Posts: 64998
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nation Chirara wrote:Thank you . . . .

That's a pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!