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

Is this the right forum ?

David Fishman
Greenhorn

Joined: Dec 28, 2003
Posts: 18
Hi all,
Awhile back, I posted the questions below regarding implicit narrowing conversions but unfortunately haven't received any replies. Would asking these questions be more appropriate in the intermediate forum ? The questions were more why's than how to's but I seem to recall seeing similar discussions on other topics.
Thanks !

My Java text mentions the following as conditions that must hold for implicit narrowing conversions on assignment
  • the source is a constant expression of either byte,short,char or int
  • the destination is either byte,short or char
  • the value of the source is determined to be in the range of the destination at compile time


  • Then it goes on to say that no implicit narrowing conversions whatsoever are allowed on passing to method parameters.
    My first question is - why only allow types narrower than long ? Why not allow (for example) a constant double value that is in range ?
    My second question is - why not allow any narrowing at all for parameter passing ? My understanding is that parameters are really created only when methods are called at runtime, but if the rules above hold wouldn't it make sense to be consistent with the treatment of assignments as is the case in other operations ?
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    My first question is - why only allow types narrower than long ? Why not allow (for example) a constant double value that is in range ?
    In a nutshell, because that's what the original language and compiler designers decided. Perhaps because someone lost a bet, flipped a coin, or it was actually a little bit easier or faster to implement the compiler in such a manner.
    I don't think that it would be terribly difficult to add a little extra functionality to a compiler to allow it to consider double and long values for possible automatic narrowing upon assignment. But it would be just another little neat thing that could mildly complicate the compiler implementation, as well as mildly slow it down.


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    My second question is - why not allow any narrowing at all for parameter passing ? My understanding is that parameters are really created only when methods are called at runtime, but if the rules above hold wouldn't it make sense to be consistent with the treatment of assignments as is the case in other operations ?
    I'd imagine that a few folks feel it's not appropriate for a compiler to do any automatic narrowing. If you want a float, make it a float by casting it.
    I'd guess that it's entirely possible and conceivable to create a compiler that would allow automatic narrowing of larger data type values known at compile time, which are passed as arguments to a method. But, again, this would likely complicate the compiler implementation mildly, as well as slow it down a bit.
    So, again, these aspects of the Java programming language and compiler are this way because that's just how the original language and compiler designers wanted it - probably because it was easier and faster to implement.
    Anurag Mishra
    Ranch Hand

    Joined: Jun 16, 2003
    Posts: 121
    Hi,
    This is right forum....
    thanks
    Anurag
    Sadanand Murthy
    Ranch Hand

    Joined: Nov 26, 2003
    Posts: 382
    Originally posted by Dirk Schreckmann:
    My second question is - why not allow any narrowing at all for parameter passing ? My understanding is that parameters are really created only when methods are called at runtime, but if the rules above hold wouldn't it make sense to be consistent with the treatment of assignments as is the case in other operations ?
    I'd imagine that a few folks feel it's not appropriate for a compiler to do any automatic narrowing. If you want a float, make it a float by casting it.
    I'd guess that it's entirely possible and conceivable to create a compiler that would allow automatic narrowing of larger data type values known at compile time, which are passed as arguments to a method. But, again, this would likely complicate the compiler implementation mildly, as well as slow it down a bit.
    So, again, these aspects of the Java programming language and compiler are this way because that's just how the original language and compiler designers wanted it - probably because it was easier and faster to implement.

    Narrowing a larger to a smaller data type can cause data to be truncated; possible even lose precision. You don't want a compiler to make such decisions if the losing of precision or data truncation can cause serious problems. It makes more sense to force the programmer to think about the data type and let the programmer decides if the data type should be narrowed.


    Ever Existing, Ever Conscious, Ever-new Bliss
    David Fishman
    Greenhorn

    Joined: Dec 28, 2003
    Posts: 18
    Thanks to all who replied !
    David Fishman
    Greenhorn

    Joined: Dec 28, 2003
    Posts: 18
    This is a reply to a thread from a few months back, however I thought I would share something I found recently in the JLS regarding not allowing implicit narrowing conversions for parameter passing. Basically the prior replies were on the right track, and here is what section 5.3 of the JLS indicates :

    ...Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process...

    More details can be found in the JLS.
    I will also post the same reply in a related thread I started Jan 1, 2004 titled �implicit narrowing conversions� just for completeness.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Is this the right forum ?