• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Duplicate local variable x

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book (Jeanne Boyarsky and Scott Selikoff, OCA Java SE 8 Programmer I Study Guide, page 82) says that the reason that this block of code will execute:

and this one won't:

is because x is repeated in the initialization block after already being declared before the loop.

If that's true, why the hell does this code work without any compilation errors:
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the declarations are outside of the for-loop header, not inside.

This:

declares both y and x as long values in the for-loop header, even though "long" is only specified for y, it is implied that x is long as well. You can't have mixed types when you declare multiple loop variables. If you're going to declare a type for the loop variables, it has to be specified for the first variable and it will be applied to any other loop variables you list that haven't already been previously declared.

This does not declare any types for y and x in the for-loop header:

Here, y and x are declared outside of the for-loop and those are the types that will be applied to them. Again, you can use variables of different types as loop variables, you just can't declare different types in the for-loop header.

Try this:

See what happens if you try to do this instead:
 
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stuff before the first ; in a for statement can be either a declaration of local variable(s), or a list of expressions.  But not both.  So you can either declare both x and y right there for the first time, or you can use an x and y that are already declared previously.  You can't do both.

Was going to write more, but I see Junilu has just posted.
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Because the declarations are outside of the for-loop header, not inside. . . .

Remember you are reading a cert exam book, not a programming tutorial. Since cert exams test your knowledge of the rules of Java®, they are allowed constructs like declaring loop variables outside the loop,, and declaring multiple variable together, which would otherwise be regarded as very poor style.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic