posted 4 years ago
Hi everyone,
I'm now on page 82 of the OCA Guide, where the authors exemplify tricky loop variations to watch our for for the exam.
the code: (I'm omitting the code within the curly braces, as it's not relevant for my doubt):
Example 3. Redeclaring a Variable in the Initialization Block
My doubt: in both variation we say that int x=0 and x=4, so why do we talk about a duplicate definition in the first variation, but not in the second variation?
(I have somewhat an idea but I'm not sure:
a) I know variables in the initialization block must be of the same type.
b) So, I'm assuming that on the first variation x=4 is interpreted as a long, because of long y = 0; (?) Is numeric promotion happening here? And if so, I had the wrong idea of numeric promotion: I though it would only happen where there is some operation between the two variables, but it's more general to the expression.
c) So, on the second example by taking long y = 10 outside of the initialization expression, we are allowing x=4 to remain an int, and therefore, not be considered a duplicate declaration but only a reassignment to value 4?)
Thanks,