2021-03-17

运算符

Java语言支持如下运算符:优先级()

  • 算数运算符:+,-,*,/,%(取余运算,即模运算),++,–
package operator;

public class Dome01 {
    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,默认输出int取整数,所以输出0
        System.out.println(a/(float)b);
        System.out.println(a/(double)b);
    }
}

30
-10
200
0
0.5
0.5

Process finished with exit code 0
package operator;

public class Dome02 {
    public static void main(String[] args) {
        long a = 123123123123L;
        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
        System.out.println((double)c+d);//int
        //不同类型运算,只要一个为long,输出的结果类型也为long,没有long时,结果都为int
    }
}

123123123264
141
18
18.0

Process finished with exit code 0

package operator;

public class Dome03 {
    public static void main(String[] args) {
        //关系运算符的返回结果:正确或错误  布尔值
        //if
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);  //   c / a   21 / 10 = 2 ... 1(模运算)

    }
}

false
true
false
true
1

Process finished with exit code 0

package operator;

public class Dome04 {
    public static void main(String[] args) {
        //++  --  自增,自减  一元运算符
        int a = 3;
        System.out.println(a);

        int b = a++;//执行完这段代码后,先给b赋值,再自增。
        //a =a + 1
        System.out.println(a);
        //a++   a = a + 1
        int c = ++a;//执行完这段代码前,先自增,再赋值给c。

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        /*我们先说一下前自增和后自增、前自减和后自减的区别。
自增(++):将变量的值加1,分前缀式(如++i)和后缀式(如i++)。前缀式是先加1再使用;后缀式是先使用再加1。
自减(--):将变量的值减1,分前缀式(如--i)和后缀式(如i--)。前缀式是先减1再使用;后缀式是先使用再减1。
         */
        /* 对于以下,有int x = 5, y = 6, z;
题目1:z = ++x + y++;
题目2:z = ++x + x++;
题目3:x = ++x + x++;

对于上面的三道题目,我们下面一一解答。使用的技巧就是:把原始计算式转化成多个、有先后计算顺序的、小的计算式,然后带入变量的值,进行求解。记住:同一优先级的运算符的计算顺序是从右往左。
Q1:z = ++x + y++;  可以转化为:
              x = x +1;
              z = x + y;
              y = y + 1;
带入x = 5, y = 6,可得x = 6; z = 12; y = 7;

Q2:z = ++x + x++;  可以转化为:
              x = x +1;
              z = x + x;
              x = x + 1;
带入x = 5,可得x = 6; z = 6+6=12; x = 7;   故x=7,z=12;

Q3:x = ++x + x++;  可以转化为:
              x = x +1;
              x = x + x;
              x = x + 1;
带入x = 5,可得x = 5+1=6; x = 6+6=12; x = 12+1=13;   故x=13。
         */
    }
}

3
4
5
3
5

Process finished with exit code 0

package operator;

public class Dome05 {
    public static void main(String[] args) {
        //幂运算   2^3  2*2*2 = 8  很多运算我们会使用工具类计算。
        double pow = Math.pow(3, 2);
        System.out.println(pow);

    }
}

9.0

Process finished with exit code 0

  • 赋值运算符=

  • 关系运算符:>,<,>=,<=,==(等于),!=(不等于),instanceof

  • 逻辑运算符&&(与),||(或),!(非)

    package operator;
    
    public class Dome05 {
        public static void main(String[] args) {
            //幂运算   2^3  2*2*2 = 8  很多运算我们会使用工具类计算。
            double pow = Math.pow(3, 2);
            System.out.println(pow);
            //逻辑运算符
            //&& 与  ||或  !非
            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));//如果是真则变为假,如果是假则变为真
            //短路运算
            System.out.println("a && b :"+(b&&a));//即b已经为假了,后面的&&a就不用经过运算了,直接输出假
            int c =5;
            boolean d = (c<4)&&(c++<4);
            System.out.println(d);
            System.out.println(c);//输出c等于5,二不是6,则证明c++未执行。即c<4短路
        }
    }
    
    9.0
    a && b :false
    a || b :true
    !(a && b)true
    a && b :false
    false
    5
    
    Process finished with exit code 0
    
    
  • 位运算符:&,|,^,~,>>,<<,>>>(了解!!!)

    public class Dome06 {
        public static void main(String[] args) {
            /*
            A = 0011 1100
            B = 0000 1101
            --------------------------------------------------------
            A&B = 0000 1100  如果两个都为1,则为1,否则为0
            A|B = 0011 1101  只要有一个为1,则为1,否则为0
            A^B = 1111 0010  相同则为0,不同则为1
            ~B = 1111 0010 取反
            2*8 = 16 怎么运算最快   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
        }
    }
    
    
    16
    
    Process finished with exit code 0
    
    
  • 条件运算符:?,:

    public class Dome08 {
        public static void main(String[] args) {
            // x ? y : z
            //如果x==true,则结果为y,否则结果为z
            int score = 80;
            String type = score <60 ?"不及格":"及格";//必须掌握
            System.out.println(type);
        }
    }
    
    
    及格
    
    Process finished with exit code 0
    
  • 扩展赋值运算符:+=,-=,*=,/=

    public class Dome07 {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
            a+=b;//a = a+b  即a=10+20=30
            a-=b;//a = a-b  即a=30-20=10
            System.out.println(a);
            //字符串连接符   +  ,String  字符串在前面则拼接,后面则运算
            System.out.println(a+b);
            System.out.println(""+a+b);//输出1020
            System.out.println(a+b+"");//输出30
        }
    }
    
    
    10
    30
    1020
    30
    
    Process finished with exit code 0
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值