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


Java Long和BigInteger的區別用法及代碼示例

在Java中,long和BigInteger是兩種不同的數據類型,用於表示和對大整數執行操作。在本文中,我們將了解 Java 中的 BigInteger 和 Long。

Java 中的 long

long 是 Java 中的一種基本數據類型,用於存儲 64 位有符號整數。

  • 它的值範圍為 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807。
  • long 是一種固定大小的數據類型,這意味著它為每個變量分配固定數量的內存。

long 數據類型通常用於預期大於 int 範圍的整數值。

用法:

long variableName;

Java long 的示例

下麵是long在Java中的實現,下麵提到:

Java


// Java Program to implement Long  
import java.io.*; 
  
// Driver Class 
public class LongExample { 
      // main function 
    public static void main(String[] args) { 
        // declaring and initializing a long variable 
        long num1 = 1234567890L; 
          
        // performing arithmetic operations with long values 
        long num2 = 9876543210L; 
        long sum = num1 + num2; 
        long difference = num2 - num1; 
        long product = num1 * num2; 
        long quotient = num2 / num1; 
          
        // printing the results 
        System.out.println("num1 = " + num1); 
        System.out.println("num2 = " + num2); 
        System.out.println("sum = " + sum); 
        System.out.println("difference = " + difference); 
        System.out.println("product = " + product); 
        System.out.println("quotient = " + quotient); 
    } 
}
輸出
num1 = 1234567890
num2 = 9876543210
sum = 11111111100
difference = 8641975320
product = -6253480962446024716
quotient = 8


Java 中的BigInteger

BigInteger 是 Java 的 java.math 包中的一個類,表示 arbitrary-precision 整數。

  • 它可以處理幾乎無限大小的整數,僅受可用內存的限製。
  • BigInteger 對象的大小不固定,它們根據需要動態分配內存。

用法:

BigInteger num1 = new BigInteger();

BigInteger 的示例

下麵是上述方法的實現:

Java


import java.io.*; 
import java.math.BigInteger; 
  
public class BigIntegerExample { 
    public static void main(String[] args) { 
        // creating BigInteger objects 
        BigInteger num1 = new BigInteger("12345678901234567890"); 
        BigInteger num2 = new BigInteger("98765432109876543210"); 
  
        // performing arithmetic operations with BigInteger objects 
        BigInteger sum = num1.add(num2); 
        BigInteger difference = num2.subtract(num1); 
        BigInteger product = num1.multiply(num2); 
        BigInteger quotient = num2.divide(num1); 
  
        // printing the results 
        System.out.println("num1 = " + num1); 
        System.out.println("num2 = " + num2); 
        System.out.println("sum = " + sum); 
        System.out.println("difference = " + difference); 
        System.out.println("product = " + product); 
        System.out.println("quotient = " + quotient); 
    } 
}
輸出
num1 = 12345678901234567890
num2 = 98765432109876543210
sum = 111111111011111111100
difference = 86419753208641975320
product = 1219326311370217952237463801111263526900
quotient = 8


long 和 BigInteger 之間的區別

long 和 BigInterger 之間的區別如下表所示:

特征

BigInteger

數據類型

Primitive

Class

尺寸

固定(64 位)

Dynamic

範圍

Limited

幾乎無限製

精確

受尺寸限製

Unlimited

內存分配

Fixed

Dynamic

溢出處理

環繞

不換行並優雅地處理溢出

用法

適合常見的整數需求

適用於非常大的整數和專門的算術運算

性能

由於 hardware-level 支持,通常速度更快

由於動態分配和方法調用而速度較慢



相關用法


注:本文由純淨天空篩選整理自mguru4c05q大神的英文原創作品 Difference Between Long and BigInteger in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。