| Author |
implicit casting of final variables
|
Jared Cope
Ranch Hand
Joined: Aug 18, 2004
Posts: 243
|
|
Hi, I've come across this particularly evil example of code: So what will happen here? We all know that an int can't be assigned to a byte without an explicit cast, so obviously the code won't compile. However, to my surprise it does! However, if you take out the final modifier for the i variable, the code won't compile. Moreover, if i gets assigned 200 (something larger than the range of a byte) and is final then it won't compile. I'm just wondering what is happening behind the scenes. Is the compiler reclassifying i as a byte variable since it knows the value at compile time and knows that it can't change during runtime with the final modifier?
|
SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
|
 |
chowdary Thammineedi
Ranch Hand
Joined: Aug 16, 2004
Posts: 126
|
|
Hi Jared The compiler will automatically narrow down the assignment. Dan Chisholm on his website explains this very well. Visit his site and your doubt's about automatic conversions will be DONE forever. The rule is that in an assignment statement if the right hand side of the assignment is a COMPILE TIME CONSTANT of type byte,char,shot or int then it will be automatically converted(narrowed) to the reference type of the left hand side. Chowdary
|
 |
kapil munjal
Ranch Hand
Joined: May 11, 2004
Posts: 298
|
|
Hi, I think, COMPILE TIME CONSTANTS means final variables. well, I think I am right about this, but if I am wrong then please correct me. Thanks Kaps
|
Kapil Munjal
SCJP 1.4, SCWCD 1.4
|
 |
Akshatha Nayak
Ranch Hand
Joined: Jul 15, 2004
Posts: 53
|
|
|
r final variables the only COMPILER TIME CONSTANT.. ??
|
Only those who will risk going too far, Can possibly find out how far one can go! <br />happiness is journey and not destination<br /> <br />A Nayak<br />----------------------------------<br />SCJP 1.4<br />SCWCD 1.3<br />SCBCD 1.3
|
 |
Stefan Guilhen
Ranch Hand
Joined: Jul 31, 2004
Posts: 61
|
|
No. Not only final variables are compiler time constants. You can express a compiler time constant as a literal too. For example: Both case clauses are valid, because the arguments are compiler time constants, one represented by a literal and one by a final variable. Notice that the following does not compile: The preceding code does not compile, because althoght x is a final variable it is not a compiler time constant, since its value will only be determined at runtime when the method amethod is invoked. Hope that helps.
|
SCJP 1.4, SCBCD 1.3<br />IBM 141, 484, 486
|
 |
Robert Chisholm
Ranch Hand
Joined: Jul 18, 2004
Posts: 69
|
|
As a corollary to the above post, this won't compile either. Although it's a bit off-topic, I notice it in a few mock exams.
|
SCJP 1.4<br />(WIP) SCJD B&S v2.3.3
|
 |
kapil munjal
Ranch Hand
Joined: May 11, 2004
Posts: 298
|
|
Hi Robert, could you tell me why there will be no implicit narrowing cast to int in your example. Please explain this concept. Thanks Kaps
|
 |
Stefan Guilhen
Ranch Hand
Joined: Jul 31, 2004
Posts: 61
|
|
The rules for variable assignments do not apply to arguments passed to methods. I can remember that Dan's mock has an explanation about this... it seem the developers of Java chose not to perform an implicit cast in values passed as arguments because it would be dificult to determine which method should be called in case of overloading. Imagine this scenario: As you can see, problems may arise here if the implicit casting is performed in arguments to methos. The value of x fits in an int, but its type is a long.... what should the compiler do to determine the method the programmer wants to be called? Stefan. [ August 20, 2004: Message edited by: Stefan Guilhen ]
|
 |
 |
|
|
subject: implicit casting of final variables
|
|
|