Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes When can you use the comma operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When can you use the comma operator" Watch "When can you use the comma operator" New topic

When can you use the comma operator

tyrone cabero

Joined: Oct 04, 2003
Posts: 4
if u have any idea please help me for my final project
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

The Java language specification makes no mention of a "comma operator;" Java doesn't have one like C does. But I do know what the question means.
You can use a comma to separate variables in a single declaration, like

but that's not considered an instance of the comma operator. You can do the same thing in a for loop:

but that's still not it. The one place in Java where a comma can be used outside of a variable declaration or argument list is in for-loop initialization and increment code, like this:

That's the closest thing to C's comma operator there is in Java. Even so, it's not very close, because the values of any expressions are discarded and not used for anything.

[Jess in Action][AskingGoodQuestions]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Comma is one of the separators in Java, and it is used to define the formal grammar of the language. For a complete set of rules, see 18.1 The Grammar of the Java Programming Language. From the rules, you can identify all the cases when comma is used as a separator.
I agree. Here's the link:
subject: When can you use the comma operator