运算符细节讲解

1.算数运算符+,-,*,/,%,++,--

2.赋值运算符=

3.关系运算符>,<,>=,<=,==,!=instanceof

4.逻辑运算符&&,||,!

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;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);//0,需转换
        System.out.println(a/(double)b);//0.5
    }
​
}
​
30
-10
200
0
0.5
​
​
​
package operator;
​
public class Demo02 {
    public static void main(String[] args) {
        long a=1234565424242L;
        int b=129;
        short c=10;
        byte d=8;
        System.out.println(a+b+c+d);//long 1234565424383 只要有long就为long,其余int
        System.out.println(b+c+d);//int 147
        System.out.println(c+d);//int 18
    }
}
​
1234565424389
147
18
​
​
package operator;
​
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符 返回ture or false
        int a=10;
        int b=20;
        int c=21;
        System.out.println(c%a);//取余    1
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}
​
1
false
true
false
true
​
​
package operator;
​
public class Demo04 {
    public static void main(String[] args) {
        //++ -- 自增自减一元运算符
        int a=3;
        int b=a++;//执行完这行代码前,先给b赋值,在自增
        //a=a+1
        System.out.println(a);//4
        //a=a+1
        int c=++a;
        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5
        //幂运算 2^3 2*2*2=8 很多运算,我们会使用一些工具类操作
        double pow = Math.pow(2, 3);//ctrl+alt+v快速生成左边
        System.out.println(pow);//8.0
​
    }
}
​
4
5
3
5
8.0
​
package operator;
​
public class Demo05 {
    public static void main(String[] args) {
        //与(and)或(or)非(取反)
        boolean a=true;
        boolean b=false;
        System.out.println("a && b:"+(b&&a));//逻辑与运算:两真才true
        System.out.println("a||b"+(a||b));//逻辑或:一真就true
        System.out.println("!(a&&b)"+!(a&&b));//逻辑非:真就变为false,假就变为true
​
        //短路运算  &&
        int c=5;
        boolean d=(c<4)&&(c++<4);//前面false,&&就短路,后面(c++<4)不用看
        System.out.println(d);//false
        System.out.println(c);//5
    }
}
​
a && b:false
a||btrue
!(a&&b)true
false
5
​
package operator;
​
public class Demo06 {
    public static void main(String[] args) {
        /*
        * A=0011 1100
        * B=0000 1101
        * ------------------------
      * A&B=0000 1100//都是1才为1,否则为0
      * A|B=0011 1101//都是0才为0,否则为1
      * ~B=11110010//取反
      * 2*8=16如何算的最快 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   //结论; << *2    推理:>>2 /2
        * */
        System.out.println(2<<3);//16
    }
}
​
16
​
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);//10
        //字符串连接符 +,string
        System.out.println(""+a+b);//1020 字符串在前面才连接在一起
        System.out.println(a+b+"");//30
        System.out.println(a+""+b);//1020
​
    }
}
​
10
1020
30
1020
​
package operator;
​
public class Demo08 {
    public static void main(String[] args) {
        //三元运算符
        //x?y:z
        //如果x==true就为y否则为z
        int score=50;
        String type=score< 60 ?"不及格":"及格";
        System.out.println(type);
    }
}
​
不及格

猜你喜欢

转载自blog.csdn.net/wanggang182007/article/details/121024420