Operating with Big Integer Values - Java Language Basics

Java examples for Language Basics:BigInteger

Description

Operating with Big Integer Values

Demo Code

import java.math.BigInteger;

public class Main {
  public static void main(String[] args) {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");
    System.out.println(bi1);//w w  w.  j  a v a2 s  .  co m
    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);
    System.out.println(bi2);
    
    bi1 = bi1.add(bi2);
    System.out.println(bi1);
    bi1 = bi1.multiply(bi2);
    System.out.println(bi1);
    bi1 = bi1.subtract(bi2);
    System.out.println(bi1);
    bi1 = bi1.divide(bi2);
    System.out.println(bi1);
    bi1 = bi1.negate();
    System.out.println(bi1);
    int exponent = 2;
    bi1 = bi1.pow(exponent);
    System.out.println(bi1);
  }
}

Result


Related Tutorials