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 ]
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Joined: Feb 02, 2006
Does not compile too. [ September 07, 2006: Message edited by: wise owen ]
Joined: May 02, 2006
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.
Joined: Aug 03, 2002
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.