JavaSe基础06:运算符、Math类以及面试题(笔记)

视频学习地址

点我跳转ψ(`∇´)ψ

前言

计算机里面最重要的是计算,而计算过程中,需要用到运算符来操作。

运算符

  • 算数运算符:+,-,*,/,%,++,–
  • 赋值运算符:=
  • 关系运算符:>,<,>=,<=,==,!=instanceof
  • 逻辑运算符:&&,||,!,~,∧,>>,<<,>>>(了解即可)
  • 位运算符:&,|,
  • 条件运算符:?:
  • 扩展赋值运算符:+=,-=,*=,/=
基本运算符
public class Demo{
	public static void main(String[] args){
		//这是一个二元运算符,按Ctrl+D可以复制当前行到下一行
		int a = 10;
		int b = 10;
		int c = 10;
		int d = 10;
		int f = 21;
		
		//取余也叫模运算
		System.out.println(f%a);	//21/10=2...1 结果应该是等于1

		System.out.println(a+b);	//结果是30
		System.out.println(a-b);	//结果是-10
		System.out.println(a*b);	//结果是200
		System.out.println(a/b);	//结果是0  本来应该等于0.5 但是舍去末尾了
		System.out.println((double)a/b);	//所以需要进行强制转换 其结果等于0.5
	}
}
运算结果类型
public class Demo{
    public static void main(String[] args){
        double a = 1;
        float b = 2;
        long c = 3L;
        int d = 4;
        char e = 5;
        short f = 6;
        byte g = 7;

        System.out.println(a+b+c+d+e+f+g);	//double类型 结果是28.0
        System.out.println(b+c+d+e+f+g);	//float类型 结果是27.0
        System.out.println(c+d+e+f+g);	//long类型 结果是25
        System.out.println(d+e+f+g);	//int类型 结果是22
        System.out.println(e+f+g);	//int类型 结果是18
        System.out.println(f+g);	//int类型 结果是13
		//总结:如果是用比int还要高的容量进行定义,那么输出值就是容量最高的那个类型的值,如果没用,则会自动转换为int类型。
	}
}
关系运算符
public class Demo{
    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
    }
}
自增自减
public class Demo{
    public static void main(String[] args){
        //关系运算符返回的结果是布尔值
        int a = 3;
        int b = 3;
        int a2 = a++;	//先输出后再加 a2 = a = a + 1
        int b2 = ++b;	//先加后再输出 b2 = b + 1 = b

		System.out.println(a);	//结果是4
        System.out.println(a2);	//结果是3
        System.out.println(b2);	//结果是4
    }
}
Math类的幂运算
public class Demo{
    public static void main(String[] args){
        //幂运算 2的3次方 2*2*2
        double pow = Math.pow(2,3);
        System.out.println(pow);	//结果是8
    }
}
逻辑运算符
public class Demo{
    public static void main(String[] args){
        //与(and) 或(or) 非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println(a && b); //结果是false
        System.out.println(a || b); //结果是true
        System.out.println(!(a && b));  //结果是true
        System.out.println(!(a || b));  //结果是false

        //短路运算
        int c = 5;
        boolean d = c < 4 && c++ < 4;
        System.out.println(d);  //&&逻辑运算时,如果前面是false,那么后面的运算将不会运行,直接返回false
        System.out.println(c);  //如果后面的运算执行了,那么此时的c应该是6,如果为5,那么可以说明没有执行
    }
}
位运算
public class Demo{
    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
        A^B = 0011 0001;    上下相同则为0,否则为1
        ~A = 1100 0011; 取A的反值
         */
    }
}
面试题:2*8怎么运算最快
public class Demo{
    public static void main(String[] args){
        /*
        <<左移 >>右移   相当于左移是乘以2 右移是除以2
        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0011   3
        0000 0100   4
        0000 0101   5
        0000 0110   6
        0000 0111   7
        0000 1000   8
        0001 0000   16
         */
        System.out.println(2<<3);   //所以往左移动3位就是16 这样的效率极高 直接跟底层的二进制打交道
    }
}
拓展赋值运算符+连接符
public class Demo{
    public static void main(String[] args){
        int a = 10;
        int b = 20;

        System.out.println(a+=b);	//a=a+b     a=10+20=30
        System.out.println(a-=b);	//a=a-b     上面的a是30 所以是a=30-20 =10

        //字符串连接符 +
        System.out.println(""+a+b);     //会将后面的也转换为String类型,然后拼接字符串 结果是1020
        System.out.println(a+b+"");     //会依旧进行运算 结果是30
    }
}
三目运算符
public class Demo{
    public static void main(String[] args){
        /*
        x ? y : z   如果x == true,则为y,如果为false,则为z
         */
        int score = 80;
        String type = score < 60 ? "不及格":"恭喜你及格了";  //返回的值是String类型,所以要定义为String类型
        System.out.println(type);
    }
}
运算符的优先级

运算符的优先级

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值