• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anomaly in primitive assignments in Java

 
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While studying for my SCJP 6 certification upgrade using the book "Sun Certified Programmer for Java 6 Study Guide" by Kathy & Bert, I came across something that seems anomalous. I have described it in my blog entry Primitive Assignments in Java. Here is what I find a little strange.

On page 192 of the book, it says that is legal because the compiler automatically narrows the literal value to a byte. However, on page 196, I found that will not compile. I checked this with the default compiler in NetBeans 6.7.1 and it is true. Why does the compiler not automatically narrow the double literal to a float here, just as it does for an int literal to a byte? Just wondering if there is any good reason for this, or is this just an unnoticed bug in the compiler?

Thanks...
- Ajoy
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is standard practice for the compiler. Have a look in the Java™ Language Specification and see whethr it tells you anything useful.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a gander at the JLS for Assignment Conversions which specifically talks about assignment narrowing conversions. Other than stating that the variable must be of the integral types* it is pretty silent about why double can't be narrowed to a float.

My guess is that a double expression will almost always fall outside the representable values of float because they aren't different in just their range of values but also in the precision of those values. Since the precision of a float is different than the precision of a double then the narrowing conversion might substantially change the value being assigned.

*Actually, floating point variables as the type for an assignment narrowing conversion are allowed, but only in the context of Value Set Conversions which isn't really applicable to your question
 
Ajoy Bhatia
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Campbell & Steve, for the explanation, and also for the reminder about the JLS. Should remember to look there first before asking. Anyway, the precision difference seems to be a plausible reason for this.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome Remember the JLS is not usually an easy read.
 
Ajoy Bhatia
Greenhorn
Posts: 21
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You're welcome Remember the JLS is not usually an easy read.



That I did find out Tried to figure out the concept of Value Set conversion, just reading what it said in the Assignment Conversions section, but decided after a while that I need to read some other parts to get the full context. Don't worry, I'll figure it out. If not, I'll be back to ask.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:I took a gander at the JLS for Assignment Conversions which specifically talks about assignment narrowing conversions. Other than stating that the variable must be of the integral types* it is pretty silent about why double can't be narrowed to a float.



The JLS doesn't talk about long either -- so no implicit narrowing for long compile time constants to int, short, etc.

Henry
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic