轉自:
http://www.java265.com/JavaCourse/202203/2493.html
運算子簡介:
運算子用于執行程式代碼運算,會針對一個以上運算元專案來進行運算<br />
例:88+99,其運算元是88和99,而運算子則是“+”,在vb2005中運算子大致可以分為5種型別:算術運算子、連接運算子、關系運算子、賦值運算子和邏輯運算子,
下文筆者講述java中冒號運算子的功能簡介說明,如下所示:
冒號運算子的功能:
1.跳轉
2.三元運算式
3.迭代回圈
4.斷言
5.switch
6.方法(jdk8)
例
1 跳出標簽
label: for (int i = 0; i < x; i++) {
for (int j = 0; j < i; j++) {
//業務代碼
}
}
2 三元條件
int a = (b < 4)? 7: 8; // if b < 4, set a to 7, else set a to 8
3 每個回圈
String[] ss = {"hi", "there"}
for (String s: ss) {
print(s);
}
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 User {
public static int compareByAge(User a, User b) {
return a.birthday.compareTo(b.birthday);
}}
}
Arrays.sort(users, User::compareByAge);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500351.html
標籤:Java
上一篇:day07-
