Hi, maybe because its like byte b = 100 ; or the return type is a compile time constant ( it must be one of those reason )... you can try with 'return 128;' if you get an compiler error...(i am not home so i can't test ) arno
The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Joined: Jan 19, 2006
I've just tried to use 128 and I got a compile error. "Possible loss of precision".
My doubt here is whether argument passing and returning values have different rules.
As per above code, it seems the rules are different. [ April 14, 2006: Message edited by: Edisandro Bessa ]
The return value of #2 can be determined at compile time, since you use a constant value. The compiler determines that expression 100 is assigment compatible with the return type of the method, then the compiler does an automatic narrowing conversion.
The other case (#3) is different. Becuase you explicitly return a variable of type integer. The value of the variable at run time cannot be determined by the compiler. Hance, automatic narrowing conversion cannot be applied.
Regards, Edwin Dalorzo.
PS. See the Java Language Specification, look for "Assigment Conversion" in the second edition it is the 66 page.
Joined: Dec 31, 2004
See this example of a compile time constant expression, which is a variation of your third case and which does compile:
In this case the value of the expression myInt is compile time constant expression (JLS 15.28) and its value can be determined at compile time.