BigInteger class

In this chapter you will learn:

  1. How to use Java BigInteger class
  2. How to compare with BigInteger constant value

BigInteger

BigInteger is an immutable class that represents a signed integer of arbitrary precision.

BigInteger declares three convenience constants: ONE, TEN, and ZERO. Each constant is the BigInteger equivalent of 1, 10, and 0.

import java.math.BigInteger;
/*from  ja v a 2 s .co m*/
public class Main {
  public static void main(String[] args) {
    BigInteger numberB = BigInteger.TEN;

  }
}

Compare with BigInteger constant value

We can compare BigInteger value against the constant value.

import java.math.BigInteger;
/*from j  av a  2s .  com*/
public class Main {
  public static void main(String[] args) {
    System.out.println(factorial(new BigInteger("170")));
  }

  static BigInteger factorial(BigInteger n) {
    if (n.equals(BigInteger.ZERO))
      return BigInteger.ONE;
    else
      return n.multiply(factorial(n.subtract(BigInteger.ONE)));
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to create BigInteger from byte array
  2. How to create BigInteger from String
  3. How to use different radix to create BigInteger from String
  4. How to create BigInteger with random number
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format