12

What does the colon mean in Java? I have this:

public static List<String> findAllAnagrams(List<String> words) {
    List<String> result = new LinkedList<String>();
    for(String i : words){
        for (String j : words){
            if (result.contains(i)) {
                break;
            }
            else if (i == j) {

            } else {
                if (areAnagrams(i,j)){
                    result.add(i);
                    System.out.println(result);
                }
            }
        }
    }          
    return result;
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Floyd Bostan
  • 141
  • 1
  • 1
  • 4
  • 1
    This is known as a for-each loop, you might want to google it and until now, none of the answers has named it yet. – 11684 Jan 17 '13 at 08:20
  • possible duplicate of [How does the Java for each loop work?](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) – tobias_k Jul 23 '14 at 14:34
  • 1
    The answers to this questions seem not to be complete: What is this: import hudson.model.SCMS; (...) SCMS: for (SCM scm : scmTriggerItem.getSCMs()) {something();}? – Gustave Aug 12 '15 at 13:33

5 Answers5

12

It means one thing, it is an enhanced for loop.

for (String i: words) 

means the same things as

for (int i = 0; i < words.length; i++) {
    //
}

Joshua Bloch, in Item 46 of his worth reading Effective Java, says the following:

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

The preferred idiom for iterating over collections and arrays

for (Element e : elements) {
    doSomething(e);
} 

When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand (Item 45), programmers don’t always do so.

Community
  • 1
  • 1
Abraham
  • 603
  • 7
  • 19
  • It _acts_ the same, but are they for the JVM really exact the same? – 11684 Jan 17 '13 at 08:21
  • no they are not the same. The foreach is used with collections where the length of the loop can be inferred from the number of elements available on the collection. You can do it, either way but foreach is syntactic sugar of exploiting collections. – Abraham Jan 17 '13 at 08:24
  • In that case, could you include that into your answer? The OP is probably not an experienced Java developer, and I think it's best to learn correct things from the beginning. – 11684 Jan 17 '13 at 08:51
  • 1
    Surely "for(String i: words)" doesn't mean the same thing as a for loop with "int i". Perhaps "for(int k = 0....){ String i = words[k]; // }" – AlexC May 22 '15 at 10:11
  • It does not mean just "one" thing. It is also used in java assertions assert message.equals("Success") : "Wrong message. Expected: Success. Actual: [" + message + "]"; – Russell Lego Oct 29 '20 at 18:17
5
(String i : words)

For each item in words

: to indicate iterator item and item as i

so to answer - it represents for-each loop

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
2

colon in for each loop is part of syntax, colon also appears with label

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

Don't think colon(:) means anything particularly. It's just how Java designers thought to delimit parameter and expression inside improved for loop.

for ( FormalParameter : Expression ) Statement

Check Language specification for same : http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.14.2

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
0

It is for-each loop. When you see the colon (:) read it as "in" You can find very good explanation in Oracle docs with included examples, here

Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113