3.5 运算符

运算符

  • strictfp关键字:使得浮点数运算严格按照IEEE标准来执行
3.5.1 数学函数和常量
  • 取模和取余的区别

    • %:取余、Math.floorMod:取模

    • 符号的区别:

      xyx%yMath.floorMod(x,y)
      -+-+
      +-+-
      ----
      ++++

      简记:取余符号和被除数一致、取模符号和除数一致。

    • Math类中floorMod函数的代码如下:

    public static int floorMod(int x, int y) {
            return x - floorDiv(x, y) * y;
        }
    public static int floorDiv(int x, int y) {
            int r = x / y;
            // if the signs are different and modulo not zero, round down
            if ((x ^ y) < 0 && (r * y != x)) {
                r--;
            }
            return r;
        }
    //floorDiv函数中的注释写到,该函数返回的是小于等于结果的浮点数的最大整数,如
    //floorDiv(-5,3)结果为-2(-1.3333取整)
    

    而MATLAB中取模和取余的定义如下:

    取余:rem(x,y)=x-y.*fix(x./y)

    取模:mod(x,y)=x-y.*floor(x./y)

    fix向0取整,floor向负无穷取整(即Java中的floorDiv),按照以上公式可以得到如下的结果。

    Math.floorMod(+4, -3) == -2;    (+4 % -3) == +1
    Math.floorMod(-4, +3) == +2;    (-4 % +3) == -1
    Math.floorMod(-4, -3) == -1;    (-4 % -3) == -1
    Math.floorMod(+4, +3) == +1;    (+4 % +3) == +1; 
    

    以上的内容参考了https://blog.csdn.net/nishiwodebocai21/article/details/97012993

    • 使用静态导入避免在所有的数学方法和常量名前添加前缀Math(第四章讨论)

    • StrictMath类可以确保在所有的平台上得到相同的结果

3.5.2 数值类型之间的转换(由低到高)
  • 隐式转换(自动类型转换)的规则如下

    • 数值型数据的转换:byte→short→int→long→float→double。
    • 字符型转换为整型:char→int。
    • 也可以用下面的图来表示

在这里插入图片描述

(C:\Users\10219\AppData\Roaming\Typora\typora-user-images\image-20210122184256001.png)]

可以看到,char类型只能自动转换为int以上的类型,而char类型无法和byte以及short自动转换。

部分来自http://c.biancheng.net/view/796.html

3.5.3 强制类型转换(由高到低)
  • 浮点到整数:小数部分之后直接截断、整数到浮点:小数部分补零。

  • 当超出目标类型的表示范围时会出错。

  • 使用Math.round()获得一个最接近浮点数的整数。

        /**
         * Returns the closest {@code long} to the argument, with ties
         * rounding to positive infinity.
         *
         * <p>Special cases:
         * <ul><li>If the argument is NaN, the result is 0.
         * <li>If the argument is negative infinity or any value less than or
         * equal to the value of {@code Long.MIN_VALUE}, the result is
         * equal to the value of {@code Long.MIN_VALUE}.
         * <li>If the argument is positive infinity or any value greater than or
         * equal to the value of {@code Long.MAX_VALUE}, the result is
         * equal to the value of {@code Long.MAX_VALUE}.</ul>
         */
        public static long round(double a) {
            long longBits = Double.doubleToRawLongBits(a);
            long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
                    >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
            long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
                    + DoubleConsts.EXP_BIAS) - biasedExp;
            if ((shift & -64) == 0) { 
                long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                        | (DoubleConsts.SIGNIF_BIT_MASK + 1));
                if (longBits < 0) {
                    r = -r;
                }
                return ((r >> shift) + 1) >> 1;
            } else {
                return (long) a;
            }
        }
    //从注释中可以看出,当浮点数超出long的范围时,将会返回Long类中定义的最大或者最小值。
    @Native public static final long MIN_VALUE = 0x8000000000000000L;
    @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
    
3.5.4 结合赋值和运算符
  • 当运算符得到的值类型与左侧操作符类型不同时会发生强制类型转换。

    int a = 5;
    a+=3.5// 最后a为8,与(int)(a+3.5)的结果相同
    
3.5.5 自增和自减运算符
3.5.6 关系和boolean运算符
  • “短路”方式求值
3.5.7 位运算符
  • > > >:逻辑移位、>>:算术移位
3.5.8 括号和运算符规则

在这里插入图片描述

在这里插入图片描述

3.5.9 枚举类型

详见第五章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值