• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anomaly in primitive assignments in Java

 
Ajoy Bhatia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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: 13
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome Remember the JLS is not usually an easy read.
 
Ajoy Bhatia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.

 
Henry Wong
author
Marshal
Pie
Posts: 21004
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic