Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is comma (,) operator or separator in Java?

Tags:

java

operators

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?

like image 519
Fresher Avatar asked Aug 03 '16 02:08

Fresher


1 Answers

, 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++.

like image 131
ajb Avatar answered Sep 22 '22 13:09

ajb