public class Test5
{
public static void main(String[] args)
{
//比較運算符
int x = 5;
System.out.println(x > 5);
System.out.println(x < 5);
System.out.println(x == 5);
System.out.println(x != 5);
System.out.println(x >= 5);
System.out.println(x <= 5);
//判斷左邊的對象屬於右側的類型
System.out.println("asd" instanceof String);
System.out.println("========================================>");
//邏輯運算符
// & 邏輯與
//當兩側都爲真時爲真
//當有一個爲假時爲假
//那麼考慮一個問題,如果每次我們都需要
//進行左右兩邊的對比,是不是很慢呢
System.out.println(true & true);
System.out.println(true & false);
System.out.println(false & true);
System.out.println(false & false);
System.out.println("========================================>");
//&& 邏輯與的短路
//由邏輯與的特性可以直到,一個爲假就爲假
//在考慮程序由左到右運算,那麼第一個爲假
//是不是就不用看右邊了?
//&&運算符的對比 & 會提高一點點效率,但
//僅在第一個爲假時
System.out.println(false && true);
//這個雖然有一個假,但也必須比較兩個值
//程序在拿到左邊真時並不知道後邊是假,
//所以這裏t && f 和 t & f 是一樣的
System.out.println(true && false);
System.out.println("========================================>");
// | 邏輯或
//跟 & 正好相反
//當一個爲真時就爲真
//當都爲假時才爲假
System.out.println(true | true);
System.out.println(true | false);
System.out.println(false | true);
System.out.println(false | false);
System.out.println("========================================>");
// || 邏輯或的短路
// 跟上邊那個一樣,當第一爲真時,不看後邊
System.out.println(true || false);
System.out.println("========================================>");
// ! 邏輯非
//非真爲假,非假爲真
System.out.println(!true);
System.out.println(!false);
System.out.println("========================================>");
// ^ 異或
//兩個相同時爲假,不同時爲真
System.out.println(true ^ true);
System.out.println(true ^ false);
System.out.println(false ^ true);
System.out.println(false ^ false);
System.out.println("========================================>");
//單個& |由於效率問題,基本不會用到
//這一現象僅在邏輯上,也就是布爾運算
//判斷x>2 並且 x<4
// & 方式
x = 1;
if(x > 2 && x < 4)
{
// code ...
}
//該表達式與上邊的是一樣的
if(x > 2)
{
if(x < 4)
{
// code ...
}
}
//觀察這個代碼,如果要合併的話怎麼做?
//還像上邊那樣是否還可以呢?
if(x > 2)
{
if(x < 4)
{
// code ...
}
else
{
// code ...
}
}
//仔細觀察,發現代碼不對,爲什麼呢?這裏
//的else裏,包括了x<3 和 x>3,而我們要的
//else是隻要x>3
if(x > 2 && x < 4)
{
// code ...
}
else
{
// code ...
}
//這樣雖然正確,但達不到我們的預期目的,也
//就是說合並不了,這個問題實際上是作用域的
//問題,合併邏輯表達式必須要內部作用域的
//表達式是唯一作用域
if(x > 2 && x < 4)
{
// code ...
}
else if( x > 3)
{
// code ...
}
//邏輯表達式的合併要考慮2方面內容,第一是否
//內部作用域唯一,第二,是否其他的作用域會被
//擴展,也就是說以後會用的
System.out.println("========================================>");
//考慮一個問題,這個表達式什麼情況下進if?
//是真的話進if,假進else對吧,那麼我想反
//過來怎麼辦?只能把表達式變成x<=5?
x = 6;
if(x > 5)
{
// code ...
}
else
{
// code ...
}
//邏輯非就相當於把你邏輯調換過來
//就像中國開車右側行駛,有些國家
//卻是左側行駛一樣
if(!(x > 5))
{
// code ...
}
else
{
// code ...
}
//假設我們用bool表示電源的開關,默認是關閉的
//那麼這個功能應該怎麼寫?
boolean bool = false;
//看起來寫的不錯,沒有問題,但是不是有點麻煩?
//一個應用程序中的bool標識符很多,表示很多東西
//如果都這樣寫是不是會累死?
if(bool)
{
//關閉電源的代碼
bool = false;
}
else
{
//打開電源的代碼
bool = true;
}
//我們知道電源開關就兩種狀態,開和關,所以才用
//布爾來表示,而每次按下開關,電源的狀態是取非
//這裏的邏輯非也算一種簡便用法
if(bool)
{
//關閉電源的代碼
}
else
{
//打開電源的代碼
}
bool = !bool;
System.out.println("========================================>");
//是人都知道同性戀必須是同性在一起對吧?誰反對?
//假設我們用true表示男人,false表示女人
//那麼我們現在是民政局,如果兩個人p1 和 p2 不是
//同性戀我們允許他們結婚,如果是同性戀就不行
boolean p1 = true;
boolean p2 = true;
//正常情況下我們都會這麼寫
if(p1 != p2)
{
// 不是同性戀,允許結婚
}
else
{
// 是同性戀不允許結婚
}
//異或的功能是兩個不一樣的爲真,相同爲假,也就是
//說p1 ^ p2 如果爲真,那就不是同性戀。
//當然由於對比布爾表達式來說,你用!= 和 ^ 都可以
//但是相信我,如果你用 ^ 絕對是亮點,很多老程序員
//不經意間看見這個絕對愣神
if(p1 ^ p2)
{
// 不是同性戀,允許結婚
}
else
{
// 是同性戀不允許結婚
}
}
}
複習筆記4 比較運算符
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.