當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java 8 BigInteger shortValueExact()用法及代碼示例


Java 8中引入了java.math.BigInteger.shortValueExact()。這是一個內置函數,可將BigInteger的值轉換為short值並檢查丟失的信息。如果BigInteger的值大於32,767或小於-32,768;該方法將拋出ArithmeticException,因為BigInteger不能在短範圍內使用。

用法:

public short shortValueExact()

返回值:此方法返回此BigInteger的short值。


異常:如果BigInteger的值大於32,767或小於-32,768,則該方法引發ArithmeticException。因為此範圍不適用於短距離範圍。

例:

Input:15,564
Output:15,564
Explanation:15,564 is given as input which is BigInteger
and short value of 15,564 is 15,564

Input:-17,584
Output:-17,584
Explanation:-17,584 is given as input which is BigInteger 
and short value of -17,584 is -17,584

Input:40000
Output:ArithmeticException
Explanation:When 40000 is tried to convert to short,
since 40000 > 32,767 (greater than a short's range), 
therefore it throws an arithmetic exception.

以下示例程序旨在說明BigInteger類的shortValueExact()方法:

程序1:演示正數<32,767的shortValueExact()方法

// Java program to demonstrate shortValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Creating a BigInteger object 
        BigInteger big; 
        big = new BigInteger("15564"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value:"
                           + big); 
  
        // convert big to the short value 
        short b1 = big.shortValueExact(); 
  
        // print short value 
        System.out.println("short converted value:"
                           + b1); 
    } 
}
輸出:
BigInteger value:15564
short converted value:15564

程序2:演示shortValueExact()方法用於負數> -32,768

// Java program to demonstrate shortValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("-17584"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value:"
                           + big); 
  
        // convert big to the short value 
        short b1 = big.shortValueExact(); 
  
        // print short value 
        System.out.println("short converted value:"
                           + b1); 
    } 
}
輸出:
BigInteger value:-17584
short converted value:-17584

程序3:演示shortValueExact()方法用於負數<-32,768。它將引發算術異常

// Java program to demonstrate shortValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("-40000"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value:"
                           + big); 
  
        try { 
            // convert big to the short value 
            short b1 = big.shortValueExact(); 
  
            // print short value 
            System.out.println("short converted value:"
                               + b1); 
        } 
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}
輸出:
BigInteger value:-40000
Exception:java.lang.ArithmeticException:BigInteger out of short range

程序4:演示用於大於32,767的正數的shortValueExact()方法。它將引發算術異常

// Java program to demonstrate shortValueExact() 
// method of BigInteger Class 
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Creating a BigInteger object 
        BigInteger big = new BigInteger("40000"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value:"
                           + big); 
  
        try { 
            // convert big to the short value 
            short b1 = big.shortValueExact(); 
  
            // print short value 
            System.out.println("short converted value:"
                               + b1); 
        } 
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}
輸出:
BigInteger value:40000
Exception:java.lang.ArithmeticException:BigInteger out of short range

參考: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#shortValueExact-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Java 8 | BigInteger shortValueExact() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。