• 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

narrowing

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do know that from an int down to a byte you can have implicit narrowing if the value is in the appropriate range.

However -- are doubles and floats treated seperately on this issue?
i.e. can a double also be narrowed implicitly to a float?
There seems to be a relationship between byte and short and char and int regarding implicit narrowing.
Is there a relationship between doubles and float regarding implicit narrowing.
If not -- why not?
float f = 1.0;
This does not compile?
Why can't it be narrowed implicitly seeing as the value is within the range for a float?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The permitted implicit narrowing conversions of integral constant expressions to smaller integral types can never result in loss of precision.

Casting a double constant to a float may involve loss of precision, not only in obvious cases like "1.23456789012" but in subtle cases like ".3". It is not good to have language rules that can only be verified by running a program or by knowing hexadecimal fractions really well.

On the other hand, allowing implicit narrowing conversions of doubles to floats, only when there is no possible loss of precision, would make for lots of challenging exam questions and entertaining threads on this BB. Maybe I should suggest it to James Gosling if I ever meet him.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Conversion between byte,short and char is always a narrowing
conversion
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Any Conversion between byte,short and char is always a narrowing
conversion


Not any conversion:

byte -> short always widening
[ March 21, 2005: Message edited by: Mike Gershman ]
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the correction Sir,the correct statement is

All conversions between char and the two integer types
byte and short are narrowing.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I do know that from an int down to a byte you can have implicit narrowing if the value is in the appropriate range.



How is this code giving compilation error implicit conversion was possible.

 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicit integer narrowing requires a constant expression. A literal is one example of a constant expression, a final variable is another.

In this case, "final int i = 1;" will let the code compile.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by deshdeep divakar:
... All conversions between char and the two integer types byte and short are narrowing...


Correct.

Ref: JLS, 5.1.3 Narrowing Primitive Conversions...
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic