aspose file tools*
The moose likes Beginning Java and the fly likes Anomaly in primitive assignments in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anomaly in primitive assignments in Java" Watch "Anomaly in primitive assignments in Java" New topic
Author

Anomaly in primitive assignments in Java

Ajoy Bhatia
Greenhorn

Joined: Jun 02, 2004
Posts: 13
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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


Steve
Ajoy Bhatia
Greenhorn

Joined: Jun 02, 2004
Posts: 13
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
You're welcome Remember the JLS is not usually an easy read.
Ajoy Bhatia
Greenhorn

Joined: Jun 02, 2004
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anomaly in primitive assignments in Java