Java基礎知識精通各種運算符
前言
本文章主要集中講解運算符,篇幅較長,請耐心看完絕對通俗易懂。
一、算數(shù)運算符
1.簡介
再Java中,使用算術運算符 +、-、*、/、%分別代表加減乘除,取模。
2.運用
+
在java中+有三種:1、正常的運算。2、作為正負來用。3、作為連接符(任意數(shù)據(jù)類型的數(shù)據(jù)與字符串相連接的時候,那個+號就是連接符)
實例:
package com;
public class liu {
public static void main(String[] args) {
//實例一:作為正常運算
int c = 1;
int d = 2;
int e = c + d;
System.out.println(e) //最后結果為3;
//實例二:作為連接符
//已知有兩個變量a和b,值分別為1和2,在控制臺上輸出a + b = 3, 1 + 2 = 3
int a = 1;
int b = 2;
System.out.println(a + " + " + b + " = " + (a + b));
//控制臺顯示結果為:a + b = 3。
//注意此時的"+","="作為字符串,而(a + b)則進行運算。
}
}
-
在java中+有兩種:1、正常的運算。2、作為正負來用。
實例:
package com;
public class liu {
public static void main(String[] args) {
int a = 1;
int b = 3;
int c = b-a;
System.out.println(c); //運算結果:2。
}
}*
實例:
package com;
public class liu {
public static void main(String[] args) {
int i = 4;
int j = 2;
int a = i*j;
System.out.println(a);//8
}
}/
當參與/運算的兩個數(shù)都是整數(shù)的話,結果為整數(shù),反之為浮點數(shù)。
實例:
package com;
public class liu {
public static void main(String[] args) {
//實例1:作為整數(shù)運算
int i = 4;
int j = 2;
int a = i / j;
System.out.println(a);//2
//實例2:作為浮點運算
int i = 5;
double j = 2;
double a = i / j;
System.out.println(a);//2.5
}
}%
整數(shù)的取余操作(有時稱為取模)用%表示。
package com;
public class liu {
public static void main(String[] args) {
int i = 2;
int j = 3;
System.out.println(i % j); //0
int a = 4;
int b = 2;
System.out.println(a % b); //2
}
}二、自增自減運算符
++
單獨使用:不管++在前還是在后,結果都是一樣,詳見實例1。
參與運算:++在前:先自身加1,然后再參與運算,詳見實例2;++在后:先參與運算,然后再自身加1,詳見實例3。
實例:
package com;
public class liu {
public static void main(String[] args) {
//實例1:單獨運算。
int i = 1;
i++; // i = i + 1
++i;
System.out.println(i);//結果都等于2。
//實例2:++在前。
int i = 1;
int j = ++i;
System.out.println(j);//2
System.out.println(i);//2
//實例3:++在后。
int i = 1;
int j = i++;
System.out.println(j);//1
System.out.println(i);//2
}
}–
單獨使用:不管–在前還是在后,結果都是一樣,詳見實例1。
參與運算:–在前:先自身減1,然后再參與運算,詳見實例2;–在后:先參與運算,然后再自身減1,詳見實例3。
package com;
public class liu {
public static void main(String[] args) {
//實例1:單獨運算。
int i = 2;
i--; // i = i - 1
--i;
System.out.println(i);//結果都等于1。
//實例2:--在前。
int i = 1;
int j = --i;
System.out.println(j);//1
System.out.println(i);//1
//實例3:--在后。
int i = 1;
int j = i--;
System.out.println(j);//2
System.out.println(i);//1
}
}
三、賦值運算符
可以在賦值中使用二元運算符,形式非常簡便:x += 4 等價于 x = x + 4。
常見的賦值運算符有:=、+=、-=、*=、/=、%=。
下面以+=為例:
package com;
public class liu {
public static void main(String[] args) {
int i = 1;
i += 2; // i = i + 2
System.out.println(i); //輸出結果3
byte b = 1;
b += 2; // b = (byte)(b + 2)
System.out.println(b);
}
}
四、關系運算符
關系運算符得出來的結果一定是boolean類型的數(shù)據(jù),也就是說要么是true,要么是false
常見的關系運算符:>、<、>=、<=、!=、==。
實例:
package com;
public class liu {
public static void main(String[] args) {
int i = 3;
int j = 2;
System.out.println(i > j);//true
System.out.println(i < j);//false
System.out.println(i >= j);//true
System.out.println(i <= j);//false
System.out.println(i != j);//true
System.out.println(i == j);//false
// ==是比較兩邊的大小,=是賦值。
}
}
五、邏輯運算符
左右兩邊的數(shù)據(jù)的數(shù)據(jù)類型都是boolean類型,結果都是boolean類型。
&(單與)
兩邊只要有一個是false,結果就為false
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(true & true);
System.out.println(true & false); //false
System.out.println(false & false);
System.out.println(false & true); //false
}
}
| (單或)
兩邊只要有一個是true,結果就為true。
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(true | true);
System.out.println(true | false);
System.out.println(false | false); //false
System.out.println(false | true);
}
}
^(異或)
兩邊相同為false,兩邊不同為true。
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(true ^ true); //false
System.out.println(true ^ false);
System.out.println(false ^ false); //false
System.out.println(false ^ true);
}
}!(非)
直接就是取反。
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(!true); //false
System.out.println(!false); //true
System.out.println(!!true); //true
System.out.println(!!!true); //false
}
}&&(雙與)
兩邊只要有一個是false,結果就為false。
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(true && true);
System.out.println(true && false); //false
System.out.println(false && false); //false
System.out.println(false && true); //false
}
}&和&&的區(qū)別?
- &&:如果左邊為false,右邊就不再執(zhí)行了,結果就為false。
- &:如果左邊為false,右邊依然會執(zhí)行,結果就為false。
|| (雙或)
兩邊只要有一個是true,結果就為true。
實例:
package com;
public class Demo12 {
public static void main(String[] args) {
System.out.println(true || true); //true
System.out.println(true || false); //true
System.out.println(false || false); //false
System.out.println(false || true); //true
}
}|和||的區(qū)別?
- ||:如果左邊為true,右邊就不再執(zhí)行了,結果就為true。
- |:如果左邊為true,右邊依然會執(zhí)行,結果就為true。
六、位運算符
兩邊的數(shù)據(jù)都是數(shù)字,結果也為數(shù)字。
常見的位運算符:&(與位運算)、|(或位運算)、^(異或位運算)、~(按位取反)、>>(右移位運算)、<<(左移位運算)、>>>(無符號右移)。
實例:
package com;
public class Demo01 {
public static void main(String[] args) {
System.out.println(3 & 2); //2
System.out.println(3 | 2); //3
System.out.println(3 ^ 2); //1
System.out.println(~3);//-4
System.out.println(3 >> 2);//0
System.out.println(3 << 2);//12
System.out.println(3 >>> 2);//0
System.out.println(-3 >> 2);//-1
System.out.println(-3 >>> 2);
}
}>>和>>>區(qū)別是什么?
>>:如果數(shù)據(jù)是負數(shù),最左邊的符號位是1,右移之后,左邊要補1。
如果數(shù)據(jù)是正數(shù),最左邊的符號位是0,右移之后,左邊要補0。
>>>:不管數(shù)據(jù)是正數(shù)還是負數(shù),右移之后,左邊都補0。
七、三元運算符
格式:條件表達式 ? 表達式1 : 表達式2; 等同于 x > y ? x : y
條件表達式的結果一定是boolean類型
執(zhí)行流程:
如果條件表達式為true,就會執(zhí)行表達式1,不會執(zhí)行表達式2
如果條件表達式為false,就會執(zhí)行表達式2,不會執(zhí)行表達式1
實例:
package com;
public class Demo02 {
public static void main(String[] args) {
//獲取兩個數(shù)的較大值
int i = 2;
int j = 3;
//第一種:
int max = i > j ? i : j;
System.out.println(max); //3
//第二種:
System.out.println(i > j ? i : j);
//表達式1和表達式2既然會得到一個結果,如果傳遞給一個變量去接收,該變量的數(shù)據(jù)類型應該和表達式1和表達式2的結果的數(shù)據(jù)類型匹配。
//浮點型:
double d = 3 > 2 ? 1 : 2;
System.out.println(d); //輸出:1.0
//字符型:
char c1 = 3 > 2 ? 97:98;
System.out.println(c1); //輸出:a
}
}到此這篇關于Java基礎知識精通各種運算符的文章就介紹到這了,更多相關Java運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯誤的解決辦法
今天小編就為大家分享一篇關于IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯誤的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
Thymeleaf是一個現(xiàn)代的服務器端 Java 模板引擎,適用于 Web 和獨立環(huán)境。Thymeleaf 的主要目標是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板,本文就來用它實現(xiàn)一個前端表格,感興趣的可以了解一下2022-10-10

