Java中Math类方法简介

我想对于Math类大家一定很熟悉了,是Java提供的一个用来进行简单数学运算的工具类。对于Math类来说,常用的方法有:

  • 加法
  1. public static int addExact(int x, int y);求两个int类型整数的和
  2. public static long addExact(long x, long y):求两个long类型整型数的和

加法的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 加法
        System.out.println("3 + 4 = " + Math.addExact(3, 4));
        System.out.println("5L + 4L = " + Math.addExact(5L, 4L));
    }
}

执行结果如下图所示:

  • 减法
  1. public static int subtractExact(int x, int y):求两个int类型整数的差
  2. public static long subtractExact(long x, long y):求两个long类型整数的差

减法的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 减法
        System.out.println("6 - 4 = " + Math.subtractExact(6, 4));
        System.out.println("7L - 2L = " + Math.subtractExact(7L, 2L));
    }
}

执行结果如下图所示:

  • 乘法
  1. public static int multiplyExact(int x, int y):求两个int类型整数的积
  2. public static long multiplyExact(long x, int y):求一个long类型整型数和一个int类型整数的积
  3. public static long multiplyExact(long x, long y):求两个long类型整型数的积

乘法的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 乘法
        System.out.println("6 * 4 = " + Math.multiplyExact(6, 4));
        System.out.println("7L * 2 = " + Math.multiplyExact(7L, 2));
        System.out.println("7L * 5L = " + Math.multiplyExact(7L, 5L));
    }
}

执行结果如下图所示:

  • 除法
  1. public static int floorDiv(int x, int y):求两个int类型整数的相除的结果
  2. public static long floorDiv(long x, int y):求一个long类型整型数除以一个int类型整数的结果
  3. public static long floorDiv(long x, long y):求两个long类型整型数相除的结果

除法的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 除法
        System.out.println("6 / 4 = " + Math.floorDiv(6, 4));
        System.out.println("7L / 2 = " + Math.floorDiv(7L, 2));
        System.out.println("7L / 5L = " + Math.floorDiv(7L, 5L));
    }
}

执行结果如下图所示:

  • 求余
  1. public static int floorMod(int x, int y):求两个int类型整数的取模
  2. public static int floorMod(long x, int y):求一个long类型整型数对另一个int类型整数的取模
  3. public static long floorMod(long x, long y):求两个long类型整型数的取模

求余的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 求余
        System.out.println("6 % 4 = " + Math.floorMod(6, 4));
        System.out.println("7L % 2 = " + Math.floorMod(7L, 2));
        System.out.println("7L % 5L = " + Math.floorMod(7L, 5L));
    }
}

执行结果如下图所示:

  • 取反
  1. public static int negateExact(int a):对一个int类型的整数进行取反
  2. public static long negateExact(long a):对一个long类型的整数进行取反

取反的示例代码如下所示:

public class MathTest {

    public static void main(String[] args) {

        // 取反
        System.out.println("3取反的结果为:" + Math.negateExact(3));
        System.out.println("-5L取反的结果为:" + Math.negateExact(-5L));
    }
}
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值