Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning long for an integer

 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it is alright to return a constant integer from a method where a short or a byte is expected, why does this not work with returning a constant long in place of an integer?

For eg. the following is fine
However this doesent work out:


A possible loss of precision error is thrown.
Similarly, returning a constant double for a float is also disallowed. Does this have anything to do with the float-value set and double-value set?
[ September 07, 2006: Message edited by: Aniket Patil ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint:

Does not compile too.
[ September 07, 2006: Message edited by: wise owen ]
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is true. Though i didn't state it clearly, i was only referring to constants that could be fit in the data type. However, my doubt is still not clear w.r.t int and constant long.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check JLS 5.2 and you will find:
In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int :

* A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.


So if your constant expression is a long, you are out of luck - you cannot assign it to the narrower return type.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic