DAY 7

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;
        int c = 21;
       
        System.out.println(c % a);//1
        System.out.println(a + b);//30
        System.out.println(a - b);//-10
        System.out.println(a * b);//200
        System.out.println(a /(double) b);//0.5
    
}
package operator;
​
public class Demo02{
    public static void main(String[]args){
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        
        System.out.println(a+b+c+d);//123123123123264
        System.out.println(b+c+d);//141
        System.out.println(c+d);//18
    }
        
    }

注:第一个输出的是long,后面两个可以看见输出的是int。因此只有long输出的是long,其余的无论是啥类型都输出为int

package operator;
​
public class Demo03{
    public static void main(String[]args){
        //关系运算符
        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
    }
}

首先关系运算符返回的结果,只有正确和错误。而正确和错误只能用布尔值来表示

自增 自减预算符和math的一点涉及

package operator;
​
public class Demo04{
    public static void main(String[]args){
    //一元运算符
        int a = 3;
        int b = a++;
        //a++: 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);
        System.out.println(pow);
        
    }
}

注:a++的意思:a = a + 1 只是和 ++a 的区别在于 a++是先赋值后再加一 ,比如:上面的编程所示a的值赋于了b,然后在进行a = a +1的运算,但是输出是b 而b的值是最开始啊赋予的,因此是3 而++a是先进行a = a+1 的运算再进行赋值 比如上面的编程所示a先进行了两次a =a+1的运算然后再赋予给c,因此c输出的值为5

逻辑预算符,位运算符

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);
        System.out.println(c);
        //出现短路的问题就是前面‘d = (c<4)’这个地方就已经是错位的所以就不能进行后面的运算,因此报错
    }
}

注:1. 在逻辑与运算:两个变量都为真,结果才为true 2. 在逻辑或运算:两个变量有一个为真,则结果才为true 3.在逻辑非运算:真的就变成假,假的就变成真

package operator;
​
public class Demo06{
    public static void main(String[]args){
        /*
        A = 0011 1100
        B = 0000 1101
        
        A&B = 0000 1100 //上下相加或相对比11得1,其余为0
        A/B = 0011 1101//上下相加或相对比00得0,其余为1
        A^B = 0011 0001//上下相加或相对比相同为0,不同为1
        ~B = 1111 0010//上下相加或相对比取反0为1,1为0
        
        */
        System.out.println(2<<3);//16
        /*0000 0000   0
          0000 0001   1
          0000 0010   2
          0000 0011   3
          0000 0100   4
          0000 1000   8
          0001 0000   16
        <<---*2    >>----/2
        */
    }       
}

条件预算符,拓展赋值预算符

首先条件预算符也叫三元预算符

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);//1020
        //字符串运行后,后面的进行拼接
        System.out.println(a+b+"");//30
        //先进行加法的运算,在进行字符串运行
    }     
}

注:"字符串"代表着字符串

package operator;
​
public class Demo08{
    public static void main(String[]args){
        //x ? y : z
        //如果x==ture,则结果为y,否则结果为z
        int score = 50;
        String type = score < 60 ?"不及格":"及格";
        System.out.println(type);    
    }
}

相同的方法还有就像 if算法

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值