Search code examples
javagrailsgroovy

Why does ":" at the last of a groovy statement does not throw any error?


I mistakenly wrote the following in the groovy console but afterwards I realized that it should throw error but it did not. What is the reason behind groovy not throwing any error for colon at last of the statement?Is it allocated for documentation or sth like that?

    a:
    String a
    println a

This threw no error when i tried executing this code in https://groovyconsole.appspot.com/


Solution

  • It's a label, just like it would be in Java. For example:

    a:
    for (int i = 0; i < 10; i++)
    {
        String a = "hello"
        println a
    ​    break a; // This refers to the label before the loop
    }​