• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Implicity Casting in Java

 
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79978
397
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie for the informative answer, now I got it all.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nation Chirara wrote:Thank you . . . .

That's a pleasure
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic