What does the colon (:) operator do?

Apparently a colon is used in multiple ways in Java. Would anyone mind explaining what it does? For instance here:

String cardString = ""; for (PlayingCard c : this.list) //
How would you write this for-each loop a different way so as to not incorporate the : ? 12.2k 60 60 gold badges 55 55 silver badges 83 83 bronze badges asked Mar 8, 2010 at 6:09 23k 36 36 gold badges 85 85 silver badges 113 113 bronze badges

Others have already mentioned that such case is a for-each loop. For a more detailed explanation of how it works, see java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

Commented Mar 8, 2010 at 6:31

12 Answers 12

There are several places colon is used in Java code:

label: for (int i = 0; i < x; i++) < for (int j = 0; j < i; j++) < if (something(i, j)) break label; // jumps out of the i loop >> // i.e. jumps to here 
int a = (b < 4)? 7: 8; // if b < 4, set a to 7, else set a to 8 
String[] ss = for (String s: ss) < print(s); // output "hi" , and "there" on the next iteration >
int a = factorial(b); assert a >= 0: "factorial may not be less than 0"; // throws an AssertionError with the message if the condition evaluates to false 
switch (type)
class Person < public static int compareByAge(Person a, Person b) < return a.birthday.compareTo(b.birthday); >> > Arrays.sort(persons, Person::compareByAge); 
107k 218 218 gold badges 149 149 silver badges 207 207 bronze badges answered Mar 8, 2010 at 6:16 Randy Sugianto 'Yuku' Randy Sugianto 'Yuku' 72.9k 58 58 gold badges 184 184 silver badges 233 233 bronze badges nice - I missed a few! and I didn't even know you can name assertions like that, very useful. Commented Mar 8, 2010 at 6:22

Coming from .NET (C#), the closest analogy for the structure in question would be the for-each, which you explained in a good manner.

Commented Sep 6, 2012 at 7:41

A failed assert does not "quit the program". It throws an AssertionError . It will only cause the program to exit if it is thrown on the stack of the only remaining non-daemon thread . and not caught.

Commented Jun 13, 2014 at 8:09 if you aim to include all, then I would add double colon (::) operator Commented Jun 23, 2015 at 6:50 Also, a double colon is used in method references. Commented Apr 14, 2016 at 3:37

There is no "colon" operator, but the colon appears in two places:

1: In the ternary operator, e.g.:

int x = bigInt ? 10000 : 50; 

In this case, the ternary operator acts as an 'if' for expressions. If bigInt is true, then x will get 10000 assigned to it. If not, 50. The colon here means "else".

2: In a for-each loop:

double[] vals = new double[100]; //fill x with values for (double x : vals) < //do something with x >

This sets x to each of the values in 'vals' in turn. So if vals contains [10, 20.3, 30, . ], then x will be 10 on the first iteration, 20.3 on the second, etc.

Note: I say it's not an operator because it's just syntax. It can't appear in any given expression by itself, and it's just chance that both the for-each and the ternary operator use a colon.