Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When can you use the comma operator

 
tyrone cabero
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sirs,
if u have any idea please help me for my final project
thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic