3

I noticed something I hadn't realized before about for loops in Java.

For example an int and a long declared before the for loop may be initialized inside the initilization block.

    int x;
    long y;
    for( x = 1,  y = 2; x < 10 && y < 10; x++, y++) {
        System.out.print(x + ",  " + y );
    }

However, those same variables will cause a compiler error if declared inside the initialization block.

    //DOES NOT COMPILE. 
    for( int x = 1, long y = 2; x < 10 && y < 10; x++, y++) {
        System.out.print(x + ",  " + y );
    }

Why is it that multiple variables declared before the initialization block can be of different data types, though variables declared inside the initialization block must be of the same type?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
  • 2
    try this `for( int x = 1, y = 2; x < 10 && y < 10; x++, y++) { System.out.print(x + ", " + y ); }` – Ousmane D. Mar 17 '18 at 12:00
  • 2
    I know that works ... those are the same 'type' – Edward J Beckett Mar 17 '18 at 12:01
  • @Aominè ~ No... that is not multiple variable *types* in the foor loop initilization block. semantics mate. – Edward J Beckett Mar 17 '18 at 12:04
  • Make them both `int` or both `long` If they can't be the same type, use more statements as you would have to do anywhere else. – Peter Lawrey Mar 17 '18 at 12:04
  • @PeterLawrey ~ Yes sir... However, the question is why the first declaration is allowed but the second fails? – Edward J Beckett Mar 17 '18 at 12:05
  • Unrelated, but declaring and mutating multiple variables isn't really idiomatic of a for loop IMO. – StuartLC Mar 17 '18 at 12:05
  • 1
    @StuartLC ~ Good point ;-) – Edward J Beckett Mar 17 '18 at 12:06
  • 1
    Because a declaration of a variable is a statement, and you only get one statement as the loop initializer. An assignment is an expression, and you can limit those with the comma within one statement. – daniu Mar 17 '18 at 12:06
  • 1
    The `for` loop only allows one declared type as everything after the `;` which would normally separate them is the condition and the iterator. – Peter Lawrey Mar 17 '18 at 12:07
  • Theres the answer then ... – Edward J Beckett Mar 17 '18 at 12:08
  • @EddieB if you did read all the answers in the suggested dupe, then you would have seen the answer to your question. semantics mate. – Ousmane D. Mar 17 '18 at 12:10
  • 1
    In the end, it works that way in Java because that's the way it works in C and it works that way in C because that's what made sense to the designers at the time based on what they imaged was the use case for a simple `for` loop. – Peter Lawrey Mar 17 '18 at 12:11
  • This is not a duplicate.. This is about declaring different data types... not just muliple variables of the same type... – Edward J Beckett Mar 17 '18 at 12:12
  • 1
    @EddieB again, the suggested dupe has a question showing `for (Node current = first, int currentIndex; current != null; current = current.next, currentIndex++){...}` , how is that the same type? – Ousmane D. Mar 17 '18 at 12:13
  • @Aominè ~ I would rather have an authoritative answer as Peter Lawrey provided. – Edward J Beckett Mar 17 '18 at 12:13
  • 1
    @EddieB good, then you've got your answer as a comment. – Ousmane D. Mar 17 '18 at 12:14
  • @Aominè ~ Cool... it was really a language design question mate... – Edward J Beckett Mar 17 '18 at 12:15
  • Duplicate explains it using Java Language Specification so I don't see how is it not authoritative. JSL clearly states that basic for loop is defined as `for ( [ForInit] ; [Expression] ; [ForUpdate] ) StatementNoShortIf` where `ForInit` can be either `StatementExpressionList` (which explains why `for( x = 1, y = 2;` works fine) OR `LocalVariableDeclaration` which is defined as `{VariableModifier} UnannType VariableDeclaratorList` so it expects only one type and possibly list of arguments. So `int x, int y` is wrong (not to mention when types are different) but `int a, b, c[]) is fine. – Pshemo Mar 17 '18 at 12:47

0 Answers0