Java的知识点5——扩展赋值运算符、 关系运算符、 逻辑运算符、字符串连接符、 条件运算符
生活随笔
收集整理的這篇文章主要介紹了
Java的知识点5——扩展赋值运算符、 关系运算符、 逻辑运算符、字符串连接符、 条件运算符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
擴展運算符
程序
public class Dd1 {public static void main(String[] args) {int a=3;int b=4;a+=b;//相當于a=a+b;System.out.println("a="+a+"\nb="+b);a=3;a*=b+3;//相當于a=a*(b+3)System.out.println("a="+a+"\nb="+b); } }運行結果:
a=7
b=4
a=21
b=4
?關系運算符
- ==、!= 是所有(基本和引用)數據類型都可以使用
- > 、>=、 <、 <= 僅針對數值類型(byte/short/int/long,? float/double。以及char)
?邏輯運算符
? 短路與和邏輯與的程序:
public class Dd1 {public static void main(String[] args) {//1>2的結果為false,那么整個表達式的結果即為false,將不再計算2>(3/0)boolean c=1>2&&2>(3/0);System.out.println(c);//1>2的結果為false,那么整個表達式的結果即為false,還要計算2>(3/0),0不能做除數,//會輸出異常信息boolean d=1>2&2>(3/0);System.out.println(d);} }程序運行結果:
false
Exception in thread "main" java.lang.ArithmeticException: / by zero
?? ?at Dd1.main(Dd1.java:7)
位運算符
public class Test1 {public static void main(String[] args) {int a=3*2*2;int b=3<<2; //相當于:3*2*2int c=12/2/2;int d=12>>2; //相當于:12/2/2System.out.println(a+"\t"+b+"\t"+c+"\t"+d);}}程序運行結果:12?? ?12?? ?3?? ?3
注:? 1.? &和|既是邏輯運算符,也是位運算符。如果兩側操作數都是boolean類型,就作為邏輯運算符。如果兩側的操作數是整數類型,就是位運算符。
? ? ? ? ?2.?不要把“^”當做數學運算“乘方”,是“位的異或”操作。
字符串連接符
“+”運算符兩側的操作數中只要有一個是字符串(String)類型,系統會自動將另一個操作數轉換為字符串然后再進行連接
?條件運算符
public class Test2 {public static void main(String [] args) {int score=80;int x=-100;String type=score<60?"不及格":"及格"; //type=及格int flag=x>0?1:(x==0?0:-1); //flag=-1System.out.println("type="+type);System.out.println("flag="+flag);} }程序運行結果:
type=及格
flag=-1
?
總結
以上是生活随笔為你收集整理的Java的知识点5——扩展赋值运算符、 关系运算符、 逻辑运算符、字符串连接符、 条件运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA的知识点4——字符型变量/常量
- 下一篇: java美元兑换,(Java实现) 美元