In this blog we will learn how to use BigInteger  in Java.
Let’s get started!!!
    
        
            
             
         
     
    Introduction
 
BigInteger  allows us to work with large integers that exceed range of primitive type like int and long.
int has a max value of 2^31 -1.
long had max value of 2^63-1.
BigInteger can exceed value greater int and long.
    
        
            
             
         
     
    Creating BigInteger
 
We can create BigInteger by specifying the value in the constructor as shown in below example.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  bigInteger  =  new  BigInteger ( "12333585357495344853" ); 
         System . out . println ( bigInteger );  //12333585357495344853
  } 
}  
 
We can also create constant predefined BigInteger  values of one, two and ten as shown in below examples
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  one  =  BigInteger . ONE ; 
         BigInteger  two  =  BigInteger . TWO ; 
         BigInteger  ten  =  BigInteger . TEN ; 
         System . out . println ( one );  //1
  System . out . println ( two );  //2
 System . out . println ( ten );  //10
     } 
 }  
 
    
        
            
             
         
     
    Arithmetic Operations
 
    
        
            
             
         
     
    Adding 2 BigInteger numbers
 
We can add  2 BigInteger numbers by using add() method of the object.
It returns a BigInteger whose value is current object value + value to be added.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "12333585357495344853" ); 
         BigInteger  num2  =  new  BigInteger ( "454333585357434344999" ); 
         System . out . println ( num1 . add ( num2 ));  //466667170714929689852
      } 
 }  
 
    
        
            
             
         
     
    Subtracting  2 BigInteger numbers
 
We can subtract  2 BigInteger numbers by using subtract() method of the object.
It returns a BigInteger whose value is current object value - value to be subtracted.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "92333585357495344853" ); 
         BigInteger  num2  =  new  BigInteger ( "4543335853574343449" ); 
         System . out . println ( num1 . subtract ( num2 ));  //87790249503921001404
      } 
 }  
 
    
        
            
             
         
     
    Dividing 2 BigInteger numbers
 
We can divide  2 BigInteger numbers by using divide() method of the object.
It returns a BigInteger whose value is current object value / value to be divided.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "92333585357495344853" ); 
         BigInteger  num2  =  new  BigInteger ( "4543335853574343449" ); 
         System . out . println ( num1 . divide ( num2 ));  //20
      } 
 }  
 
    
        
            
             
         
     
    Multiply operation on 2 BigInteger
 
We can multiply  2 BigInteger numbers by using multiply() method of the object.
It returns a BigInteger whose value is current object value * value to be multiplied.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "3242342342" ); 
         BigInteger  num2  =  new  BigInteger ( "234234" ); 
         System . out . println ( num1 . multiply ( num2 ));  //759466816136028
      } 
 }  
 
    
        
            
             
         
     
    Advanced operations on BigIntegers
 
Operation like getting square root of number, raising number to the power of given exponent,
getting next possible prime number can also be performed as shown in below examples.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "3242342342" ); 
         System . out . println ( num1 . sqrt ());  //56941
  System . out . println ( num1 . pow ( 2 ));  //10512783862726044964
 System . out . println ( num1 . nextProbablePrime ());  // 3242342371
     } 
 }  
 
    
        
            
             
         
     
    Generating BigInteger randomly
 
Constructs a randomly  generated BigInteger , uniformly distributed over the range 0 to (2numBits - 1).
The uniformity of the distribution assumes that a fair source of random bits
is provided by random object.
It always returns non-negative BigInteger.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
  
package  examples ; 
 import  java.math.BigInteger ; 
import  java.util.Random ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( 20 ,  new  Random ()); 
         System . out . println ( num1 );  //490906
  
     } 
 }  
 
    
        
            
             
         
     
    Generating prime numbers randomly
 
We can generate random prime number  based on given bit length using probablePrime()  method
as shown in below example.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
  
package  examples ; 
 import  java.math.BigInteger ; 
import  java.util.Random ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  prime1  =  BigInteger . probablePrime ( 50 ,  new  Random ()); 
         BigInteger  prime2  =  BigInteger . probablePrime ( 3 ,  new  Random ()); 
         System . out . println ( prime1 );  //867417845917921
  System . out . println ( prime2 );  //5
     } 
 }  
 
    
        
            
             
         
     
    Comparing BigInteger using compareTo() method
 
We can compare  2 BigInteger numbers using compareTo() method as shown in below example.
It returns -1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than val respectively.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "3827238764238" ); 
         BigInteger  num2   =  new  BigInteger ( "34234234" ); 
         System . out . println ( num1 . compareTo ( num2 )); 
 
     } 
 }  
 
    
        
            
             
         
     
    Converting BigInteger to other data types
 
We can convert  BigInteger to data types like int,long and float as shown in below examples.
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "324234" ); 
         System . out . println ( num1 . intValue ());  //324234
  System . out . println ( num1 . longValue ());  //324234
 System . out . println ( num1 . floatValue ());  // 324234.0
     } 
 }  
 
    
        
            
             
         
     
    BitWise operations
 
We can perform bitwise OR , bitwise AND and bitwise XOR operations as shown in below
examples
 1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
  
package  examples ; 
 import  java.math.BigInteger ; 
 public  class  App  { 
     public  static  void  main ( String []  args )  { 
         BigInteger  num1  =  new  BigInteger ( "10" ); 
         BigInteger  num2   =  new  BigInteger ( "20" ); 
         System . out . println ( num1 . or ( num2 ));  //30
  System . out . println ( num1 . and ( num2 ));  // 0
 System . out . println ( num1 . xor ( num2 ));  // 30
     } 
 }  
 
    
        
            
             
         
     
    Conclusion
 
I hope this tutorial helps you to get complete understanding of BigInteger .
Thanks!!