Есть несколько мест, где двоеточие используется в коде Java:
1) Метка перехода (Учебное пособие):
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
2) Тернарное условие (Учебник):
int a = (b < 4)? 7: 8; // if b < 4, set a to 7, else set a to 8
3) Цикл For-each (Учебник):
String[] ss = {"hi", "there"}
for (String s: ss) {
print(s); // output "hi" , and "there" on the next iteration
}
4) Утверждение (Руководство):
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
5) Случай в операторе switch (Учебник):
switch (type) {
case WHITESPACE:
case RETURN:
break;
case NUMBER:
print("got number: " + value);
break;
default:
print("syntax error");
}
6) Ссылки на методы (Учебник)
class Person {
public static int compareByAge(Person a, Person b) {
return a.birthday.compareTo(b.birthday);
}}
}
Arrays.sort(persons, Person::compareByAge);
person
Randy Sugianto 'Yuku'
schedule
08.03.2010