Java基础学习 Day4 运算符
程序员文章站
2022-03-08 10:23:51
...
-
算数运算符
-
赋值运算符
-
关系运算符
//关系运算符返回的是结果 int a= 10; int b= 20; int c= 21; System.out.println(a>b); //false System.out.println(a<b); //true System.out.println(a==b); //false System.out.println(a!=b); //true System.out.println(c%a); //1
-
逻辑运算符
-
位运算符
-
条件运算符
//二元运算符 int a =10; int b =20; System.out.println(a+b); //30 System.out.println(a-b); //-10 System.out.println(a*b); //200 System.out.println(a/b); //0 System.out.println(a/(double)b); //0.5 long a = 123456l; int b =123; short c=10; byte d = 8; float f = 324F; System.out.println(a+b+c+d); //Long System.out.println(b+c+d); //int System.out.println(c+d); //int System.out.println((double) c+d); System.out.println(d+f); //float //一元运算符,自增自减 int a = 3; int b = a++; //先赋值后自增 //a=a+1 System.out.println(a); //4 System.out.println(b); //3 //a=a+1 int c = ++a; //先自增后赋值 System.out.println(c); //5 //幂运算 double pow = Math.pow(2,3); //2的3次方 System.out.println(pow); //8.0 //逻辑运算符 boolean a = true; boolean b = false; System.out.println("a&&b:"+(a&&b)); //false System.out.println("a||b"+(a||b)); //true System.out.println("!(a&&b):"+!(a&&b)); //true System.out.println("========="); //短路运算 int c = 5; boolean d = (c<4)&&(c++<4); //判断前面是false,就不走后面的 System.out.println(d); //false System.out.println(c); //5 //拓展运算符 int i1 =10; int i2 = 20; i1+= i2; // i1=i1+i2 System.out.println(i1); //30 //字符串连接符 System.out.println(i1+i2); //50 System.out.println(""+i1+i2); //3020 System.out.println(i1+i2+""); //50 //三元运算符 int score = 60; String type = score<70?"不及格":"及格"; System.out.println(type); //位运算 /* A = 0011 1100 B = 0000 1101 A&B 0000 1100 //遇0得0 A|B 0011 1101 //有一个1得1 A^B 0011 0001 //相同就是0 ~B 1111 0010 2*8怎么运算最快 2*2*2*2 效率极高 << 左移,乘以2 >> 右移,除以2 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 4 0000 0100 8 */ System.out.println(2<<3); //16
推荐阅读