Originally posted by gurug:
For Lalitha's example to work, we need an explicit cast, but at the same time an assignment like,
byte b = 127;
will perfectly compile. I think there is a difference between passing a value as a method argument and assigning. Even this will work,
byte b = (int)127;
Why is that so?
-GuruG.
Java performs implicit
narrowing conversion for literals provided that they fall within the legal range of the primitive type. Thus, assigning 127 to a byte is allowed because a byte can accept that much. However, assigning a value of 128 to a byte would fail without an explicit conversion. But take note that this automatic narrowing
does not apply to method invocation. It only applies to assignment. So Lalitha's example failed because automatic narrowing was not performed.
[ September 25, 2003: Message edited by: Alton Hernandez ]