Incremental Java
Comma Operator

Motivation

Look at the syntax of the for loop.
for ( init ; cond ; update )
  for body 
The semicolons in the for header are not the usual semicolons that end statements.

These semicolons are separators, that let you know where init, cond, update are located.

What if you wanted to use a for loop to print? the following:

1 10
2 9
3 8
4 7
5 6
Furthermore, you want to do this by initializing two int variables, call them first and second, to 0 and 10 respectively.

You want to keep printing as long as first < second and after each iteration, you increment first and decrement second.

This is what you'd need to do:

You can't let init be int first = 0; int second = 10 because the compiler would get confused. It sees the semicolon and thinks that you are writing a condition after the semicolon. It sees a declaration, and prints an error message.

For the same reason, you can't write update as i++ ; j--. Again, the semicolon confuses the compiler.

The solution? Use the comma operator.

Personally, I don't think the comma operator is a great idea, and I suspect it was invented just for the for loop.

Comma Operator

The comma operator is like the semicolon, but not quite. Here's the syntax.
expr1, expr2
You can have two or more expressions separated by commas. The entire thing is also an expression.

Semantics

The semantics is to evaluate the expressions left to right. The final result of the evaluation is the evaluated value of the rightmost expression.

Now think about whether this is a good idea or not.

Example

Consider the following code.
   int j = 2, k = 4 ;
   int x ;

   // Assignment statement with comma operator
   x = j + 1, k ;
Recall the semantics of an assignment statement. Evaluate the RHS, then take the value and put it in the box on the LHS.

In this case, we evaluate j + 1 to 3, and then evaluate k to 4. The final result of this comma expression is 4 which is the evaluated value of the rightmost expression.

Thus, x is assigned to 4.

Here's the problem. Why did we bother writing j + 1? It never gets used. So why is it there?

Side Effects

We only write comma expressions if each expression (except possibly the rightmost) has a side effect. That is, it should change the values of the variables in the expression.

If we don't do this, it makes little sense to have the comma operator.

The following makes a little more sense:

   x = j++, k ;
At the very least, j is updated. Even here, it makes more sense to write the code without the comma operator.
   j++ ;
   x = k ;

for loops

The comma operator makes the most sense in a for loop. Let's solve the problem we posed at the beginning of this lesson.
  for ( int first = 0, second = 10 ;
        first < second ;
        first++, second-- )
  {
     System.out.println( first + " " + second ) ;
  }
The comma operator is used only in the update. The init is a declaration. We're allowed to have a declaration in init. This prevents us from declaring variables of two different types.

We could use the comma operator in init provided we didn't declare variables. For example,

  int first, second ;
  for ( first = 0, second = 10 ;
        first < second ;
        first++, second-- )
  {
     System.out.println( first + " " + second ) ;
  }
In this case, we could have variables of different types in the init, separated by comma operators.

In the example above, we actually have two assignment statements (which are also expressions), separated by commas, not a declaration. This distinction is subtle, so I hope you noticed it.

Not a Comma Operator

The Java compiler has to be clever. Not everything that has a comma is a comma operator.

Method Calls are Not Comma Operators

For example,
   foo.bar( x, y + 3, 10 ) ;
The commas separate the arguments. It evaluates the arguments, but the result is not a single value passed to bar().

Method Signatures are Not Comma Operators

public void foo( int x, int y, double z ) ;
These commas separate parameters. The parameters aren't even expressions!

Declarations are Not Comma Operators

int x = 3, y = x, z ;
Again, commas used to separate the variables.

Comma Operator Rare

Most of the times, when you see a comma, it's not a comma operator. The most common place you see the comma operator is in a for loop header, in either the init and/or update. You rarely see it elsewhere. Even so, it's good to know what it is, so you can recognize it.