java基礎語言 運算子

來源:互聯網
上載者:User

標籤:計算   過程   print   實現   反碼   強制類型轉換   注意   練習題   static   

/*    ++,--運算子的使用:        單獨使用:            放在運算元的前面和後面效果一樣。(這種用法是我們比較常見的)        參與運算使用:            放在運算元的前面,先自增或者自減,然後再參與運算。            放在運算元的後面,先參與運算,再自增或者自減。                作用:就是對變數進行自增1或者自減1。*/public class Text3 {        public static void main(String[] args) {            //定義兩個變數            int x = 3;            int y = 4;            //單獨使用            //x++;            //y--;            ++x;            --y;            System.out.println("x:"+x+",y:"+y);//x++或者++x,單獨使用時,輸出的值都是一樣,這裡為4;y--或者--y,單獨使用時,輸出的值都是一樣,這裡為3            System.out.println("-------------------");            //參與運算使用            int a = 3;            int b = 4;                        //int c = a++;            //int d = b--;                        int e = ++a;            int f = --b;                        System.out.println("a:"+a); //4            System.out.println("b:"+b); //3            //System.out.println("c:"+c); //3            //System.out.println("d:"+d); //4            System.out.println("e:"+e); //4            System.out.println("f:"+f); //3        }}
/*    ++,--的練習題        第一題:    int a = 10;    int b = 10;    int c = 10;    a = b++;    c = --a;    b = ++a;    a = c--;    請分別計算出a,b,c的值        第二題:    int x = 4;    int y = (x++)+(++x)+(x*10);    請分別計算出x,y的值*/public class Text3 {    public static void main(String[] args) {        int a = 10;        int b = 10;        int c = 10;        a = b++; //a=10,b=11,c=10        c = --a; //a=9,b=11,c=9        b = ++a; //a=10,b=10,c=9        a = c--; //a=9,b=10,c=8                System.out.println("a:"+a);  //9        System.out.println("b:"+b);  //10        System.out.println("c:"+c);  //8        System.out.println("--------------");                int x = 4;        int y = (x++)+(++x)+(x*10);        //4+6+60        //x=5,6                System.out.println("x:"+x);  //6        System.out.println("y:"+y);  //70    }}
/*    面試題(可能性大):        short s=1;s = s+1;                 short s=1;s+=1;        上面兩個代碼有沒有問題,如果有,那裡有問題。                為什麼第二個木有問題呢?            擴充的賦值運算子其實隱含了一個強制類型轉換。                        s += 1;            不是等價於 s = s + 1;            而是等價於 s = (s的資料類型)(s + 1);*/public class Text4 {    public static void main(String[] args) {        //short s = 1;        //s = s + 1;        //System.out.println(s); //損失精度,                short s = 1;        s += 1; //好像是 s = s + 1;        System.out.println(s);  //2    }}
/*    位元運算符:        &,|,^,~        <<,>>,>>>            注意:        要做位元運算,首先要把資料轉換為二進位。*/public class Text4 {    public static void main(String[] args) {        //&,|,^,~                int a = 3;        int b = 4;                System.out.println(3 & 4);  //0        System.out.println(3 | 4);  //7        System.out.println(3 ^ 4);  //7         System.out.println(~3);     //-4    }}/*    分析:因為是位元運算,所以我們必須先把資料換算成二進位。        3的二進位:11        00000000 00000000 00000000 00000011    4的二進位:100        00000000 00000000 00000000 00000100        &位與運算:有0則0。        00000000 00000000 00000000 00000011       &00000000 00000000 00000000 00000100        -----------------------------------        00000000 00000000 00000000 00000000        結果是:0            |位或運算:有1則1。        00000000 00000000 00000000 00000011       |00000000 00000000 00000000 00000100        -----------------------------------        00000000 00000000 00000000 00000111        結果是:7            ^位異或運算:相同則0,不同則1。        00000000 00000000 00000000 00000011       &00000000 00000000 00000000 00000100        -----------------------------------        00000000 00000000 00000000 00000111        結果是:7            ~按位取反運算子:0變1,1變0        00000000 00000000 00000000 00000011       ~11111111 11111111 11111111 11111100 (補碼)              補碼:11111111 11111111 11111111 11111100       反碼:11111111 11111111 11111111 11111011       原碼:10000000 00000000 00000000 00000100        結果是:-4*/
/*    面試題:        請自己實現兩個整數變數的交換        注意:以後講課的過程中,我沒有明確指定資料的類型,預設int類型。*/public class Text4 {    public static void main(String[] args) {        int a = 10;        int b = 20;                System.out.println("a:"+a+",b:"+b);                //方式1:使用第三方變數(開發中用的)        /*        int c = a;        a = b;        b = c;        System.out.println("a:"+a+",b:"+b);        System.out.println("------------");        */                //方式2:用位異或實現(面試用)        //左邊:a,b,a        //右邊:a ^ b        /*        a = a ^ b;        b = a ^ b; //a ^ b ^ b = a        a = a ^ b; //a ^ b ^ a = b        System.out.println("a:"+a+",b:"+b);        */                //方式3:用變數相加的做法        /*        a = a + b; //a=30        b = a - b; //b=10        a = a - b; //a=20        System.out.println("a:"+a+",b:"+b);        */                //方式4:一句話搞定        b = (a+b) - (a=b); //b=30-20=10,a=20        System.out.println("a:"+a+",b:"+b);    }}

 

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.