温馨提示×

java multiply函数的用法是什么

小亿
274
2024-02-18 17:55:17
栏目: 编程语言

在Java中,multiply函数通常用于计算两个数的乘积。它可以是自定义函数,也可以是内置的方法,具体取决于代码中的实现方式。

自定义函数示例:

public static int multiply(int num1, int num2) {
    return num1 * num2;
}

public static void main(String[] args) {
    int result = multiply(5, 10);
    System.out.println("Result: " + result); // Output: Result: 50
}

内置方法示例:

public static void main(String[] args) {
    int num1 = 5;
    int num2 = 10;
    int result = Math.multiplyExact(num1, num2);
    System.out.println("Result: " + result); // Output: Result: 50
}

需要注意的是,内置方法中的Math.multiplyExact()函数会检查乘积是否超出int范围,如果超出则会抛出异常。

0