**What is Big Integer?:-**

BigInteger is one of java object in java.math package in java as part of JDK1.6.

Integer Primitive stores the number values between the range of 2 power 31 -1 -2 power 31

Long Primitive stores the numerica values between the range of 2 power 63 -1 -2 power 63.

When we are doing the Arthematic operations with either integer or long, if the result of the arthematic operation is not accomadating the their range,

saves the lower order 32 bits for integer,64 bits for long and gives the lower range result

But if we use BigInteger, it gives the correct result

BigInteger can also be used in numerious bit operations and other mathematical functions which can be store the numeric values over 2 power 64 values.

**Big Integer Example:-**

import java.math.BigInteger; public class BigIntegerDemo { public static void main(String args[]) { int integerMaximumValue = 2147483647;// This is maximum value for // integer type i.e 2 power 31 System.out.println("Case=1 = " + integerMaximumValue); System.out.println("Case=2 = " + (integerMaximumValue + 1)); System.out.println("Case=3 = " + (integerMaximumValue * 2)); System.out.println("Case=4 = " + (integerMaximumValue * 4)); // All the above cases expect Case=1 gives wrong value as overflow // occured // All the below cases gives expected values as BigInteger Object // accomdates more values BigInteger bigIntegerdemo = BigInteger.valueOf(integerMaximumValue); System.out.println(); System.out.println("Case=5 " + bigIntegerdemo); System.out.println("Case=6 " + bigIntegerdemo.add(BigInteger.ONE)); System.out.println("Case=7 " + bigIntegerdemo.multiply(BigInteger.valueOf(3))); System.out.println("Case=8 " + bigIntegerdemo.multiply(BigInteger.valueOf(4))); } }

and the following is the output

Case=1 = 2147483647 Case=2 = -2147483648 Case=3 = -2 Case=4 = -4 Case=5 2147483647 Case=6 2147483648 Case=7 6442450941 Case=8 8589934588

Attached is the methods available in BigInteger with using javap command.

This topic has been a very basic start to explore on BigInteger example. Hopefull you have enough information to get started.

If you have any questions, please feel free to leave a comment and I will get back to you.

Please click +1 button if you like this article.

## No comments:

## Post a Comment

Note: only a member of this blog may post a comment.