Java基礎三

來源:互聯網
上載者:User

標籤:void   完成   temp   符號   public   ati   括弧   demo   要求   

一、位元運算符

  之前學習的運算子,都是基於變數的,或者常量的。

執行個體1:

class test1{

public static void main(String [] args){

//&    |    ^    ~

//上面四種運算子都是基於位的。

//& : 每一位都比較,有0則0,無0則1.

// | : 每一位都比較,有1則1,無1則0.

//^ :                        ,相同則0,不同則1.

//~ : 每一位都取反。

  System.out.println(6&3);

  System.out.println(6|3);

  System.out.println(6^3);

  System.out.println(~6);

}

}

/*

============================

    110

&     011

------------------

    010

============================

    110

|         011

------------------

     111

============================

    110

^     011

------------------

    101

============================

~6:

    int 四個位元組

    00000000 00000000 00000000 00000110      6    的補碼     電腦是以補碼的形式進行計算

    11111111    11111111  11111111   111111001  ~6     的補碼

求源碼 1000000 00000000 00000000 00000111       -7 

*/

執行個體2:

class Operator_Demo2{
public static void main(String [] args){
//兩個數交換,不適用第三個變數
int x=10;
int y=5;
/*
int temp=0;
temp=x;//temp=10
x=y;//x=5
y=temp;//y=10
System.out.println(x+":"+y);
*/

/*
//如果把兩個數的和求出來,放入其中之一,然後,利用和與一個數就能求另一個數。
x=x+y;//x=15
y=x-y;//y=15-5=10=原來的x
x=x-y;//x=15-10=5=原來的y
System.out.println(x+":"+y);
*/

//^:異或運算有個特點,一個資料對另一個資料異或兩次,該資料不變
//System.out.println(5^10^10);
//System.out.println(5^10^5);
//0^0=0 0^0=0
//1^0=1 1^0=1
//0^1=1 1^1=0
//1^1=0 0^1=1

//痛點:利用^,交換x與y。
x=x^y; //x=10^5;
y=x^y; //y=10^5^5=10;
x=x^y; //x=10^5^10=5;
System.out.println(x+":"+y);

}
}

執行個體3:

class test2{

pubic static void main(String [] args){

/*

<<:左移,左邊最高位丟棄,右邊補進0

eg:0000 0001 -> 0000 0010

>>:右移,最高位是0,左邊補齊0;最高位是1,左邊補齊1.

>>>:無符號右移:無論最高位是0還是1,左邊都補齊0.

*/

System.out.println(12<<1);  //12*2的1次冪  24

System.out.println(12<<2);  //12*2的2次冪  48

/*
00000000 00000000 00000000 00001100            12的補碼
(0)000000000000000 00000000 000011000         8+16=24
(00)000000 00000000 00000000 0000110000      16+32=48
*/

System.out.println(12>>1); // 12/2的1次冪   6
System.out.println(12>>2); // 12/2的2次冪   3
System.out.println(12>>3); // 1
System.out.println(-2>>1); // -1
/*
00000000 00000000 00000000 00001100             12的補碼
000000000 00000000 00000000 0000110(0)         4+2=6
0000000000 00000000 00000000 000011(00)        2+1=3
00000000000 00000000 00000000 00001(100)     1=1

10000000 00000000 00000000 00000010          原碼  -2
11111111 11111111 11111111 11111110                 補碼   取反加一
111111111 11111111 11111111 1111111(0)             結果的補碼
100000000 00000000 00000000 0000001          結果的原碼=-1
*/

//無符號右移
System.out.println(-2>>>1);        //2147483647
/*
11111111 11111111 11111111 11111110補碼
011111111 11111111 11111111 1111111(0) 結果的補碼

*/
//寫程式計算2的8次方
System.out.println(2*2*2*2*2*2*2*2);
//普通程式員:迴圈*
//高端程式員
System.out.println(1<<8);

}

}

二、程式的結構

順序結構、條件分之結構、迴圈結構

執行個體1:

//條件分支結構-if
//if語句文法格式:
/*
if(比較運算式){
語句體;
}
運行流程:
先計算比較運算式的值,看傳回值是true還是false
如果是true,就執行語句體。
如果是false,就不執行語句體。

*/ 

class If_Demo1{
public static void main(String [] args){
int a=3;
if(a<2){
System.out.println("經過比較,a小於2");
}
System.out.println("程式執行完成");
}
}

執行個體2:

//1、比較運算式的值無論簡單還是複雜,結果必須為boolean類型。
//2、當if語句控制的語句體是一條語句,大括弧可以省略
//3、當if語句控制的語句體是多條語句,必須寫大括弧
//4、***要求,無論語句體是一條還是多條,咱們統一加大括弧!

class If_Demo2{
public static void main(String [] args){
int age=17;
if(age>=8&&age<18);    //這個語句是對的。
System.out.println("使用者屬於防沉迷群體");      

}
}

Java基礎三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.