• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit narrowing

 
Olivier Ska
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow coders,

Today I have discovered that this code would not compile because 1 is an int :
Does implicit narrowing only occur on assignments or are there other cases where we could benefit from it ?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier Ska wrote:Does implicit narrowing only occur on assignments or are there other cases where we could benefit from it ?


The rule that you are referring to, requires that the compiler knows the value -- ie. it needs to be a compile time constant.

And there is no way for a method parameter to be a compile time constant. This is why the rule is *not* part of the Java Language Specification section 5.3 -- the section that describes parameter type conversion.

Henry
 
Olivier Ska
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but could you please explain this a bit further ?

Based on your post, I thought 1 would always be a compile time constant as it is an expression composed using a literal of primitive type.
 
Olivier Ska
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also found this from the specs :
Neither strict nor loose invocation contexts include the implicit narrowing of integer constant expressions which is allowed in assignment contexts. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the rules of overload resolution (ยง15.12.2).
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier Ska wrote:Sorry but could you please explain this a bit further ?

Based on your post, I thought 1 would always be a compile time constant as it is an expression composed using a literal of primitive type.


Okay, this is a different question than the last one. Or perhaps, I interpreted your original question incorrectly.

If the question is, why isn't the whole expression considered a compile time constant, and hence, the assignment should be allowed -- that's simple. Currently, the Short class type isn't one of the data types supported by the compile time constant rules. It may in the future, but currently, as specified, it does not.

Henry
 
Olivier Ska
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize the code I provided may have been misleading.
It wasn't about the assignment at all, but only the fact that the int 1 supplied to the Short constructor couldn't be automatically narrowed to a short so that it would compile.

Actually my original question was whether implicit narrowing could occur only in assignments or elsewhere too. I think I found an example where it is also the case (pun intended ) :Thank you for your time !
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then to answer all possible options...

The original question (about constructor parameters) is handled by this section of the JLS...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3

Notice that there is no mention of compile time constant support.

The second question (which quoted my topic on compile time constants) is handled by this section of the JLS...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

Notice that there is a mention of compile time constants.

And finally, regarding the switch case statement, it is described by this section of the JLS ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11

which in turn, for the case statement, leading to constant expressions, which is described by this section of the JLS ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28

and finally, leading back to the same section, used for assignment conversions.


This is why the first does not work, and the second and the third works.
Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic