| Author |
Dan's Q doubt again?
|
Vishy Karl
Ranch Hand
Joined: Sep 08, 2003
Posts: 116
|
|
Hi Again, Answer is 22 d Compile-time error at 2. There is a compile-time error at 2. The short type variable, s, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return s;", is a return statement with an expression, s. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m4, is byte. The return statement attempts to return the value of the short type variable, s. If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte. In method m4, variable s is not a compile-time constant, so the value of variable s is not assignable to type byte. While the declaration of method m4 produces a compile-time error, the declaration of method m2 does not; because the variable is a compile-time constant with a value, 2, that is assignable to type byte. I can understand the explanation given above as "If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte." But as explained " In method m4, variable s is not a compile-time constant", now is the variable s in m4 not compile time constant as it is passed as method argument (although it is a final variable ??) Plz. clarify,I thought it to be constant Thanks,
|
"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
|
 |
Ko Ko Naing
Ranch Hand
Joined: Jun 08, 2002
Posts: 3178
|
|
We cannot return a type that is larger in range to the smaller type... Even though it is final, the word "final" has nothing to do with the type range... But if u cast the return type into byte like the following, u can compile the program.... Hope u got it...
|
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
...now is the variable s in m4 not compile time constant as it is passed as method argument (although it is a final variable ??)
Yes, the value of the variable is not known until run-time; therefore, it can not be a compile-time constant.
|
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
|
 |
Dan Gun
Greenhorn
Joined: Aug 06, 2003
Posts: 26
|
|
So it is almost in the same line as :
|
SCJP 1.4
|
 |
 |
|
|
subject: Dan's Q doubt again?
|
|
|