Java.math.BigInteger.subtract() Method

Advertisements


Description

The java.math.BigInteger.subtract(BigInteger val) returns a BigInteger whose value is (this - val).

Declaration

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

public BigInteger subtract(BigInteger val)

Parameters

  • val - value to be subtracted from this BigInteger

Return Value

This method returns a BigInteger object of value, this - val .

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

public static void main(String[] args) {

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

	bi1 = new BigInteger("123");
	bi2 = new BigInteger("-123");

	// assign difference of bi1 and bi2 to bi3
	bi3 = bi1.subtract(bi2);

	String str = "Subtraction result: " +bi1+ " - " +bi2+ " = " +bi3;

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

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

Subtraction result: 123 - -123 = 246



Advertisements
Advertisements