JAVA運算符及實例


JAVA語言支持以下運算符 優先級()

  1. 算數運算符:+,-,*,/,%,++,--

    實例1:
     package operator;
     
     public class Demo01 {
         public static void main(String[] args) {
             //二元運算符
             //Ctrl = D :賦值當前行到下一行
             int a = 10;
             int b = 20;
             int c = 25;
             int d = 25;
             int e = 35;
     
             System.out.println(a+b);
             System.out.println(c-b);
             System.out.println(a*e);
             System.out.println(d%b);
             System.out.println(d/(double)b);
             System.out.println(d/b);//精度不夠
        }
     }
    實例2
     package operator;
     
     public class Demo02 {
         public static void main(String[] args) {
             long a = 121313114123L;
             int b = 123;
             short c = 10;
             byte d = 8;
     
             System.out.println(a+b+c+d); //Long(包含long)高容量類型
             System.out.println(b+c+d); //Int
             System.out.println(c+d); //Int
             System.out.println((double) c+d); //double(包含)高容量類型
     
        }
     }
    實例3:
     package operator;
     
     public class Demo04 {
         public static void main(String[] args) {
             // ++ -- 自增 自減   一元運算
             int a =3;
     
             int b = a++; //執行完這行代碼后,先給b賦值,再自增
             //a++ a = a +1
             System.out.println(a);
             // ++a a = a +1
             int c = ++a; //執行完這行代碼前,先自增,再給b賦值
     
     
             System.out.println(a);
             System.out.println(b);
             System.out.println(c);
     
             //冪運算 2^3 2*2*2 = 8 很多運算,我們會使用一些工具類操作
             double pow = Math.pow(3,2);
             System.out.println(pow);
     
        }
     }
  2. 賦值運算符:=

  3. 關系運算符:>,<,,>=, <=, ==, !=instanceof

    實例:
     package operator;
     
     public class Demo03 {
         public static void main(String[] args) {
             //關系運算符返回結果: 正確,錯誤 布爾值
     
             int a = 10;
             int b = 20;
             int c = 21;
     
             System.out.println(a>b);
             System.out.println(a<b);
             System.out.println(a==c);
             System.out.println(b!=c);
     
        }
     }
  4. 邏輯運算符:&&,||,!

    實例:
     package operator;
     
     //邏輯運算符
     public class Demo05 {
         public static void main(String[] args) {
             //與(and) 或(or) 非(取反)
             boolean a = true;
             boolean b = false;
     
             System.out.println("a && b: "+ (a&&b));//邏輯與運算,全真為真
             System.out.println("a || b: "+ (a||b));//邏輯或運算,全假為假
             System.out.println("!(a && b): "+ !(a&&b));//真為假來假為真
     
             //短路運算
             int c = 5;
             boolean d = (c<4)&&(c++<4);
             System.out.println(c);
             System.out.println(d);
     
             boolean e = (c>4)||(c++<4);
             System.out.println(c);
             System.out.println(e);
     
        }
     }
  5. 位運算符:&,|,^,~,>>,<<,>>>

    實例:
     package operator;
     
     public class Demo06 {
         public static void main(String[] args) {
             /*
             A = 0011 1100
             B = 0000 1101
             ----------------------------
             A&B = 0000 1100
             A|B = 0011 1101
             A^B = 0011 0001
             ~B = 1111 0010
     
             2*8 = 16 2*2*2*2
             效率極高!!!
             << *2
             >> /2
     
             0000 0000 0
             0000 0001 1
             0000 0010 2
             0000 0011 3
             0000 0100 4
             0000 1000 8
             0001 0000 16
     
              */
     
             System.out.println(2<<3);
             
        }
     }
  6. 條件運算符:?,:

    實例:
     package operator;
     
     public class Demo08 {
         public static void main(String[] args) {
             //x ? y : z
             //如果x==true,則結果為y,否則結果為z
             int score = 80;
             String type = score < 60 ? "不及格":"及格"; //必須掌握
             //if
             System.out.println(type);
     
             int score1 = 50;
             String type1 = score < 60 ? "不及格":"及格"; //必須掌握
             //if
             System.out.println(type1);
     
        }
     }
  7. 擴展賦值運算符:+=,-=,*=,/=

    實例:
     package operator;
     
     public class Demo07 {
         public static void main(String[] args) {
             int a = 10;
             int b = 20;
     
             a+=b; //a = a+b
             a-=b; //a = a-b
     
             System.out.println(a);
     
             //字符串連接符 + ,String
             System.out.println(""+a+b);
             System.out.println(a+b+"");
     
        }
     }
  8.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM