Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 69373
276
  • Likes 2
  • 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: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nation Chirara wrote:Thank you . . . .

That's a pleasure
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic