• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

page 82 OCA 8 Guide: redeclaring variable / incompatible types and numeric promotion

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is simpler than you think. You are declaring a variable called x in line 1 and declaring a variable caled x in line 2. That would mean two local variables with the same name in scope simultaneously. In the second block, there is no declaration in line 3. Remember a declaration is type and variable name; if you look in line 3, you won't find any types, so you ahve assignment statements not declarations. In line 3, both x and y have the same types they had earlier.
 
Greenhorn
Posts: 29
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case you have


there is no promotion, you already have int x with int type, you cannot change type of existing variable in another declaration
this would be promotion

you declared two long variables, y and z, and int value of x will become long value of z (that doesn't mean that x variable will become of type long)

this code doesn't compile either, you cannot have ; in initialization section between those variables, it has to be , (comma)
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Remember a declaration is type and variable name;...



Hi Campbell and Ivana,

Thank you for your feedback. Simple, I understand now after having run through mazes in my head xD.
I wasn't understanding how we were informing java that x was long in the first variation: I basically forgot that we can have multiple declarations in the same line in the form of type variableA = value, variableB = value.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Esgueira wrote:. . . Thank you  . . . Thanks!

That's a pleasure
 
Ivana Kilibarda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, you're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic