Java学习-基础08运算符

运算符

  • Java语言支持以下运算符

    • 算术运算符:+ - * / % ++ --
    • 赋值运算符:=
    • 关系运算符:> < >= <= == != instanceof(面向对象的时候讲解)
    • 逻辑运算符:&& || !
    • 位运算符:& | ^ >> << >>>(了解即可!)
    • 条件运算符:? =
    • 扩展赋值运算符:*+= -= = /= %=

    基本运算符

    二元运算符

    package operator;
    
    public class Demo01 {
        public static void main(String[] args) {
            //基本运算符
            //算术运算符 二元运算符
            int a = 10;
            int b = 20;
            int c = 25;
            int d = 25;
    
            System.out.println(a+b);//30
            System.out.println(a-b);//-10
            System.out.println(a*b);//200
            System.out.println(a/b);//0,int类型,取整除,去除了小数点后面的数字
            System.out.println(a/(double)b);//0.5,将其中一个数进行强转,可以得到精确计算结果
            System.out.println(a%b);//10,取余(取模,模运算)
        }
    
    
    
    }
    

    表达式自动类型提升

    package operator;
    
    public class Demo02 {
        public static void main(String[] args) {
            long a = 2662127371721L;
            int b = 123;
            short c = 10;
            byte d = 8;
    
            System.out.println(a+b+c+d);//long类型
            System.out.println(b+c+d);//int类型
            System.out.println(c+d);//int类型,因为short类型和byte类型相加,结果会自动提升为int类型(表达式自动类型提升)
    
        }
    }
    

    自增自减运算符(一元运算符)

    package operator;
    
    public class Demo04 {
        public static void main(String[] args) {
            //算术运算符 自增++ 自减-- 一元运算符
            //自增 ++
            int a = 3;
    
            int b = a++;//b = a + 1  先赋值再自增
            int c = ++a;//c = a + 1  先自增再赋值
    
            System.out.println(a);//5
            System.out.println(b);//3
            System.out.println(c);//5
    
            //拓展:幂运算(次方运算) 2^3(Java里面没有这种表示方式) = 2*2*2     很多运算我们会使用工具类来操作!
            double pow = Math.pow(2,3);
            System.out.println(pow);//8.0
    
    
        }
    }
    

    关系运算符

    package operator;
    
    public class Demo03 {
        public static void main(String[] args) {
            //关系运算符:返回结果为布尔值,实际开发中与if语句结合使用
            int a = 10;
            int b = 20;
    
            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(a==b);//false
            System.out.println(a!=b);//true
        }
    }
    

    逻辑运算符

    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));//false,逻辑与:都为真才为真
            System.out.println("a||b="+(a||b));//true,逻辑或:一个为真即为真,否则为假
            System.out.println("!(a&&b)"+!(a&&b));//true,逻辑非:取反
    
            //短路运算
            int c = 5;
            boolean d = (c<4)&&(c++<4);
            System.out.println(d);//false
            System.out.println(c);//5,没有执行后面的c++,值依然为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);//16
        }
    }
    

    扩展赋值运算符

    package operator;
    
    public class Demo07 {
        public static void main(String[] args) {
            //扩展赋值运算符
            int a = 10;
            int b = 20;
    
            a += b; //a = a + b
            System.out.println(a);//30
    
            //字符串连接符 + String
            System.out.println(""+a+b);//3020
            System.out.println(a+b+"");//50
    
        }
    }
    

    条件运算符(三元运算符)

    package operator;
    
    public class Demo08 {
        public static void main(String[] args) {
            //条件运算符 三元运算符(必须掌握!)
            // x ? y:z
            //如果x为真,结果为y,否则为z
    
            int score = 80;
            String type = score > 60 ? "及格" : "不及格";
            System.out.println(type);//及格
        }
    }
    

    运算符优先级

    实际开发中一般都用括号()将表达式括起来,这样简洁明了,感兴趣的可以自行搜索关于Java运算符的优先级的资料,其实跟我们学习的数学运算符优先级基本一致。

posted on 2025-05-25 23:08  burgess0x  阅读(10)  评论(0)    收藏  举报