Well, to be honest, this confuses me a lot. Some books say that comma is not an operator in Java, just a separator. Other say it's both, an operator as well as a separator. What's true?
,
is not an operator in Java, as it is in C. In C, you can use the ,
operator between two expressions to cause it to evaluate both and return the second. You can't do that in Java.
You can still do this:
for (i = 1, j = 2; i < max; i++, j++) {
}
In C, the three parts of the for
header are all single expressions, and it uses the fact that ,
is an operator to allow statements like the above--i=1, j=2
is one expression, and i++, j++
is one expression. In Java, the syntax of for
has to allow for expression lists specifically, so i=1, j=2
is two expressions, as is i++, j++
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With