目錄
- Java語言支持如下運算子:
- 一、運算子優先級排列
- 二、運算子的種類
- 1)算數運算子
- 2)關系運算子
- 3)邏輯運算子
- 4)位運算子
- 5)條件運算子
- 6)擴展運算子
Java語言支持如下運算子:
- 算術運算子:+, -, * ,/,%,++,–
- 賦值運算子 =
- 關系運算子: >,<, >=, <= , ==, != instanceof
- 邏輯運算子: &&,‖,!
- 位運算子: &,|,^,~,>>,<<,>>>(了解!!!)
- 條件運算子 ? :
- 擴展賦值運算子:+=,-=,*=,/=
一、運算子優先級排列
| 優先級 | 運算子 | 簡介 |
|---|---|---|
| 1 | [ ]、 .、 ( ) | 方法呼叫,屬性獲取 |
| 2 | !、~、 ++、 – | 一元運算子 |
| 3 | * 、/ 、% | 乘、除、取模(余數) |
| 4 | + 、 - | 加減法 |
| 5 | <<、 >>、 >>> | 左位移、右位移、無符號右移 |
| 6 | < 、<= 、>、 >=、 instanceof | 小于、小于等于、大于、大于等于, 物件型別判斷是否屬于同型別 |
| 7 | == 、!= | 2個值是否相等,2個值是否不等于,下面有詳細的解釋 |
| 8 | & | 按位與 |
| 9 | ^ | 按位異或 |
| 10 | | | 按位或 |
| 11 | && | 與 |
| 12 | || | 或 |
| 13 | ? : | 條件運算子 |
| 14 | =、 += 、-= 、*= 、/=、 %=、 &=、 |=、 ^=、 <、<= 、>、>= 、>>= | 混合賦值運算子 |
二、運算子的種類
1)算數運算子
舉例1
package operator; // 新創建的包
public class Demo01 {
public static void main(String[] args) {
// 二元運算子
// ctrl +D
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b); // 0 0.5取0 加上強轉 0.5
}
}
舉例2
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12312323123232L;
int b = 123;
short c = 10;
byte d = 8;
//有 long 的型別,相加以后變為long
//有 double 的型別,相加以后變為long
//也就是說會變成最高型別的
System.out.println(a+b+c+d); //1231232321213364 Long
System.out.println(b+c+d); //141 Int
System.out.println(c+d); //18 Int
System.out.println((double) c+d); //18 Int
}
}
舉例3
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 , 自減
//一元運算子
int a = 3;
int b = a++; // b=3 a=4 先賦值,后自增
int c = ++a; // a=5 c=5 先自增,后賦值
System.out.println(a); // 5
System.out.println(b); // 3
System.out.println(c); // 5
//冪運算 2^3 2*2*2 = 8
//變 Math.pow(2,3) + alt + enter
double pow = Math.pow(2, 3);
System.out.println(pow); // 9.0
}
}
2)關系運算子
package operator;
public class Demo03 {
public static void main(String[] args) {
//關系運算子回傳的結果 : 正確,錯誤 布林值
//if
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a); //1 21/10=2````1
System.out.println(a>b); //false
System.out.println(a<b); //true
System.out.println(a==b); //false
}
}
3)邏輯運算子
package operator;
//邏輯運算子
public class Demo05 {
public static void main(String[] args) {
// 與 或 非
boolean a = true;
boolean b = false;
System.out.println("a && b"+(a && b)); //false
System.out.println("a || b:"+(a||b)); //true
System.out.println("!(a && b):"+!(a&&b)); //true
//短路運算
int c = 5;
boolean d = (c<4)&&(c++<4); //這里等于錯,不執行c++
System.out.println(d); //false
System.out.println(c); // 5
}
}
4)位運算子
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A= 0011 1100
B= 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8 = 16 2*2*2*2
最快 位運算 效率極其高
<< 左移
>> 右移
*/
System.out.println(2<<3);
}
}
5)條件運算子
package operator;
//三元運算子 條件
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
// 如果 x == true,則結果為 y, 否則結果為 z
int score = 80;
String type = score < 60?"不及格":"及格"; // 必須掌握
// if
System.out.println(type); //不及格
}
}
6)擴展運算子
package operator;
//擴展運算子
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; // a = a+b
a-=b;
System.out.println(a);
//字串連接符 + , String 字串型別
//+ 兩邊只要有一邊有 字串(String)型別,就會把他們連起來
System.out.println(a+b); //30
System.out.println(""+a+b); //1020
System.out.println(a+b+""); // 30 先加,然后運算
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/281738.html
標籤:其他
上一篇:(47)FPGA面試技能提升篇(Aurora協議/介面)
下一篇:C++ switch陳述句練習
