java中subtract函数_Java StrictMath subtractExact()用法及代碼示例

excludeExact(int num1,int num2)

excludeExact(int num1,int num2)是Java中StrictMath類的內置方法,用於獲取給定參數num1和num2的差。如果結果溢出int,則拋出異常。

由於subtractExact(int num1,int num2)是靜態的,因此對象創建不是強製性的。

用法:

public static int subtractExact(int num1, int num2)

參數:該方法接受兩個參數:

num1:第一個整數值和

num2:要從第一個整數減去的第二個整數值。

返回值:該方法返回給定參數num1和num2的差。

異常:如果結果溢出一個int,則拋出ArithmeticException。

例子:

Input: num1 = 750, num2 = 50

Output: 700

Input: num1 = 361, num2 = 929

Output: -568

以下示例程序旨在說明java.lang.StrictMath.subtractExact()方法:

示例1:

// Java praogram to illustrate the

// java.lang.StrictMath.subtractExact()

import java.lang.StrictMath;

class Geeks {

// driver code

public static void main(String args[])

{

// Get the int values

// for which the difference is required

int num1 = 76761;

int num2 = 99;

int num3 = 786616;

// Get difference between

// num1 and num2

System.out.println("Difference of " + num1

+ " and " + num2 + " = "

+ StrictMath

.subtractExact(num1, num2));

// Get difference between

// num1 and num3

System.out.println("Difference of " + num1

+ " and " + num3 + " = "

+ StrictMath

.subtractExact(num1, num3));

}

}

輸出:

Difference of 76761 and 99 = 76662

Difference of 76761 and 786616 = -709855

excludeExact(long num1,long num2)

excludeExact(long num1,long num2)是Java中StrictMath類的內置方法,用於獲取給定參數num1和num2的差。如果結果長時間溢出,則會引發異常。由於subtractExact(long num1,long num2)是靜態的,因此對象創建不是強製性的。

用法:

public static long subtractExact(long num1, long num2)

參數:該方法接受兩個參數:

num1:第一個long值和

num2:要從第一個減去的第二個long值。

返回值:該方法返回給定參數num1和num2的差。

異常:如果結果長時間溢出,則拋出ArithmeticException。

例子:

Input: num1 = 750, num2 = 50

Output: 700

Input: num1 = 361, num2 = 929

Output: -568

以下示例程序旨在說明java.lang.StrictMath.subtractExact()方法:

示例1:

// Java praogram to illustrate the

// java.lang.StrictMath.subtractExact()

import java.lang.StrictMath;

class Geeks {

// driver code

public static void main(String args[])

{

// Get the long values

// for which the difference is required

long num1 = -76342561;

long num2 = 949;

long num3 = 78326616;

// Get difference between

// num1 and num2

System.out.println("Difference of " + num1

+ " and " + num2 + " = "

+ StrictMath

.subtractExact(num1, num2));

// Get difference between

// num1 and num3

System.out.println("Difference of " + num1

+ " and " + num3 + " = "

+ StrictMath

.subtractExact(num1, num3));

}

}

輸出:

Difference of -76342561 and 949 = -76343510

Difference of -76342561 and 78326616 = -154669177

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值