六大金刚运算符---算数运算符

雨霖先森 2022-09-22 19:04:12

六大金刚运算符---算数运算符

一、算数运算符

符号:+、-、*、/、%、++、--

1.基本使用

public class 文件名{
    public static void main(String[] args){
        int num1 = 20;
        int num2 = 4;
        int  result = num1+num2;
        System.out.println(result);//24
        System.out.println(num1-num2);//16
        System.out.println(20*4);//80
        System.out.println(20/4);//5
        System.out.println(21%4);//1     取模---余数
        //--;自减1
        //-- a:先自减1,再使用
        int a = 10;
        System.out.println(--a);//9
        System.out.println(a);//9
        //b --:先使用,再自减1
        int b = 10;
        System.out.println(b--);//10
        System.out.println(b);//9
        //++:自增1
        //++c :先自增1,再使用
        int c = 10;
        System.out.println(++c);//11
        System.out.println(c);//11
        //d++:先使用,再自增
        int d = 10;
        System.out.println(d++);//10
        System.out.println(d);//11
    }
}

2.深入使用

特殊点:1byte做运算,会向上转型成int类型

注意:向上转型(自动转型)使用最高位补位

public class 文件名{
    public static void main(String[] args){
        byte b1 = 10;
        byte b2 = 20;
        //底层原理:
            //b1 - byte:0000,1010
            //      int:0000,0000,0000,0000,0000,0000,0000,1010
            //b2 - byte:0001,0100
            //      int:0000,0000,0000,0000,0000,0000,0001,0100
            //加法结果  :0000,0000,0000,0000,0000,0000,0001,1110
            //   (byte):0001,1110
            
        byte result = (byte)(b1+b2);
        System.out.println(result);//30
    }
}

特殊点2:short做运算,会向上转型成int类型

注意:向上转型(自动转型)使用最高位补位

public class 文件名{
    public static void main(String[] args){
            short s1 = 10;
            short s2 = 20;
            
            //底层原理:
            //s1 - short:0000,0000,0000,1010
            //       int:0000,0000,0000,0000,0000,0000,0000,1010
            //s2 - short:0000,0000,0001,0100
            //       int:0000,0000,0000,0000,0000,0000,0001,0100
            //加法结果  :0000,0000,0000,0000,0000,0000,0001,1110
            //   (short):0000,0000,0001,1110
            
            short result = (short)(s1+s2);
            System.out.println(result);//30
    }
}

特殊点3:除了byte或short做运算会向上转型成int类型,

其余的数值型会按照取值范围大的类型转型后再做运算符

public class 文件名{
    public static void main(String[] args){
            byte b = 10;
            short s = 10;
            int i = 10;
            long l = 10;
            float f = 10;
            double d = 10;  
            System.out.println(b + s);//int 20
            System.out.println(b + i);//int 20
            System.out.println(s + i);//int 20
            System.out.println(i + l);//long 20
            System.out.println(i + f);//float 20.0
            System.out.println(l + f);//float 20.0
            System.out.println(f + d);//double 20.0
    }
}

特殊点4: char类型做运算会获取字符的ASCII

public class 文件名{
    public static void main(String[] args){
            char c = 'a';//ASCII-97
            System.out.println(c + 1);//98
    }
}

特殊点5:++a; 和 b++;没有区别

因为分号表示执行语句的结束,不管先加还是后加,都给我加

public class 文件名{
    public static void main(String[] args){
        int a = 10;
            ++a;
            System.out.println(a);//11
            
            int b = 10;
            b++;
            System.out.println(b);//11
    }
}

3.扩展

1.为什么byte或short做运算会向上转型成int类型?

因为CPU每次去取数据按照32位取

2.特殊点:int的++i;byte的++b;没有区别

public class 文件名{
    public static void main(String[] args){
       int i = 10;
            //底层原理:i = (int)(i+1);
            ++i;
            System.out.println(i);//11
            
            byte b = 10;
            //底层原理:b = (byte)(b+1);
            ++b;
            System.out.println(b);//11
    }
}

4.经典面试题

面试题一:

public class 文件名{
    public static void main(String[] args){
        int a = 8;
        int b = (a++)+(++a)+(a*10);
        //a = 10
        //b = 8 + 10 + 10*10
        System.out.println(b);//118
    }
}

面试题二:

public class 文件名{
    public static void main(String[] args){
        int i = 0;
        /底层实现:
            //i = (int)(i+1)---1
            //i = i;
        i = ++i;
        System.out.println(i);//1
    }
}

面试题三:

public class 文件名{
    public static void main(String[] args){
        int i = 0;
        //底层实现:
        //int temp = i;//temp记录了i最初的值---0
        //i = (int)(i+1);
        //i = temp;
        i = i++;
        System.out.println(i);//0
    }
}

面试题四:

注意:++、--比+、-、*、/、%的优先级更高

public class 文件名{
    public static void main(String[] args){
        int a = 5;
        System.out.println(a++*++a);//5*7 - 35
        //5*(6+1)+1
        int a = 5;
        System.out.println(++a*++a);//6*7 - 42
        //(5+1)*(6+1)
        int a = 5;
        System.out.println(++a*a++);//6*6 - 36
        //(5+1)*6
    }
}
...全文
65 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧