Java.math.BigInteger.add() Method


Advertisements


Description

The java.math.BigInteger.add(BigInteger val) returns a BigInteger object whose value is (this + val).

Declaration

Following is the declaration for java.math.BigInteger.add() method.

public BigInteger add(BigInteger val)

Parameters

val − Value to be added to this BigInteger.

Return Value

This method returns a BigIntger object of value, this + val.

Exception

NA

Example

The following example shows the usage of math.BigInteger.add() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 3 BigInteger objects
      BigInteger bi1, bi2, bi3;  

      // assign values to bi1, bi2
      bi1 = new BigInteger("123");
      bi2 = new BigInteger("50");

      // perform add operation on bi1 using bi2
      bi3 = bi1.add(bi2);
	  
      String str = "Result of addition is " +bi3;;

      // print bi3 value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

Result of addition is 173

java_math_biginteger.htm

Advertisements