当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java 8 BigInteger byteValueExact()用法及代码示例


Java 8中引入了java.math.BigInteger.byteValueExact()。它是一个内置函数,可将BigInteger的值转换为字节,并检查是否丢失任何信息。如果BigInteger的值大于127或小于-128,则该方法将抛出ArithmeticException,因为BigInteger不在字节范围内。

用法:

public byte byteValueExact()

返回值:此方法返回此BigInteger的字节值。


异常:如果BigInteger的值大于127或小于-128,则此方法将引发ArithmeticException,因为此范围不适合字节范围。

例:

Input:122
Output:122
Explanation:122 is given as input which is bigInteger
and byte value of 122 is 122

Input:-46
Output:-46
Explanation:-46 is given as input which is bigInteger 
and byte value of -46 is -46

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

以下示例程序旨在说明BigInteger类的byteValueExact()方法:

程序1:演示byteValueExact()方法的正数<127

// Java program to demonstrate byteValueExact() 
// 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("122"); 
  
        // print value of BigInteger 
        System.out.println("BigInteger value:"
                           + big); 
  
        // convert big to the byte value 
        byte b1 = big.byteValueExact(); 
  
        // print byte value 
        System.out.println("Byte converted value:"
                           + b1); 
    } 
}
输出:
BigInteger value:122
Byte converted value:122

程序2:演示byteValueExact()方法用于负数> -128

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

程序3:演示用于负数<-128的byteValueExact()方法。它将引发算术异常

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

程序4:演示byteValueExact()方法用于正数> 127。它将引发算术异常

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

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 | BigInteger byteValueExact() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。