2

This question has already been asked, but the answers seem to be incomplete. What does the first colon in the following context mean?

import hudson.model.SCMS;
(...)
SCMS: for (SCM scm : scmTriggerItem.getSCMs()) { 
(...)

Additionally, the colon has some new uses in Java 8.

This question (which has originally been asked two years ago) is different from loop-in-java-code, because it is wider. While the answers of the original question do not mention the use of the colon as label, which is answered in question "loop-in-java-code", the latter question doesn't ask for the use of the colon within for loops nor in Java 8.

As the answer from biziclop shows, there are colon usages in the Java syntax that are easily forgotten and not mentioned in the other two questions.

Community
  • 1
  • 1
Gustave
  • 3,359
  • 4
  • 31
  • 64
  • 1
    there are two `:`. the first one is a `label` for breaking out nested loops. the second one is in `enhanced forloop`. and what do you mean other usages for `:`? both of them shown here are in java7. – Jason Hu Aug 12 '15 at 13:42
  • 1
    Duplicate? http://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-why-does-it-compile/3821841#3821841 – steenbergh Aug 12 '15 at 13:42
  • It can also be part of the `?:` operator and (from Java 8 onwards) the `::` operator. And that's as many uses as I can think of. – biziclop Aug 12 '15 at 13:43
  • the first colon -> the name of the for loop, usefull for nested loops and if you want to break or continue a different loop then you are actually in. The second is just described there. – SomeJavaGuy Aug 12 '15 at 13:43
  • @biziclop what is the `::` operator? – davioooh Aug 12 '15 at 13:44
  • @davioooh See [here](http://stackoverflow.com/questions/20001427/double-colon-operator-in-java-8?rq=1) – biziclop Aug 12 '15 at 13:44
  • It's about the first colon (edited the question adding "first") and possibly more uses that I am not aware of. – Gustave Aug 12 '15 at 13:45
  • @Gustave [it´s a label for the loop](http://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-why-does-it-compile) – SomeJavaGuy Aug 12 '15 at 13:46
  • This question is about all the possible uses of the colon, including the Java 8 ones and ones I am possibly not aware of. – Gustave Aug 12 '15 at 13:47

1 Answers1

14

There are four six uses of the : character in the Java language.

  1. To denote a label. Labels can be used to break or continue to in loops.
  2. In an enhanced for statement (also called for-each statement), which allows easy iteration across collections and arrays.
  3. As one half of the ?: conditional operator.
  4. And since Java 8, as part of the :: method reference operator.
  5. In a switch statement, after case or default.
  6. And you can also use it in an assert statement to specify an error message when the assertion fails.

In your case, SCMS: is a label, while for (SCM scm : scmTriggerItem.getSCMs()) is an enhanced for statement.

You can always look up the full syntax reference of Java here. It is amazingly dull but without it I easily missed two of the six cases.

biziclop
  • 48,926
  • 12
  • 77
  • 104