BigInteger add() Method in Java with Examples Last Updated : 04 Apr, 2019 Comments Improve Suggest changes Like Article Like Report The java.math.BigInteger.add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result. This method performs an operation upon the current BigInteger by which this method is called and BigInteger passed as the parameter. Syntax: public BigInteger add(BigInteger val) Parameters: This method accepts a parameter val which is the value to be added to this BigInteger. Return value: This method returns a BigInteger which holds sum (this + val). Below programs is used to illustrate the add() method of BigInteger. Example 1: java // Java program to demonstrate // add() method of BigInteger import java.math.BigInteger; public class GFG { public static void main(String[] args) { // BigInteger object to store result BigInteger sum; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values to calculate the sum String input1 = "5454564684456454684646454545"; String input2 = "4256456484464684864864864864"; // Convert the string input to BigInteger BigInteger a = new BigInteger(input1); BigInteger b = new BigInteger(input2); // Using add() method sum = a.add(b); // Display the result in BigInteger System.out.println("The sum of\n" + a + " \nand\n" + b + " " + "\nis\n" + sum + "\n"); } } Output: The sum of 5454564684456454684646454545 and 4256456484464684864864864864 is 9711021168921139549511319409 As you can see from above example the data is full precised even after adding number of very large length. Example 2: Java // Java program to demonstrate // add() method of BigInteger import java.math.BigInteger; public class GFG { public static void main(String[] args) { // BigInteger object to store result BigInteger sum; // double variable // To store result as double double d; // For user input // Use Scanner or BufferedReader // Two objects of String // Holds the values to sum // The number's length is of 400 digits // Which is out of range of double String input1 = "012345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4554324324362432" + "7674637264783264" + "7832678463726478" + "3264736274673864" + "7364732463546354" + "6354632564532645" + "6325463546536453" + "6546325463546534" + "6325465345326456" + "4635463263453264" + "654632498739473"; String input2 = "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345678901234567" + "8901234567890123" + "4567890123456789" + "0123456789012345" + "6789012345678901" + "2345873271893718" + "2974897146378481" + "7489127847281478" + "2174871248721847" + "8748327463756475" + "6745781641263981" + "2839721897438974" + "3286574365764576" + "2376914689217817" + "4362473624721647" + "61247612748612746"; // convert the string input to BigInteger BigInteger a = new BigInteger(input1); BigInteger b = new BigInteger(input2); // Using add() method sum = a.add(b); // Display the result in BigInteger System.out.println("The sum of\n" + a + " \nand\n" + b + " " + "\nis\n" + sum); // Using double to hold the result d = Double.parseDouble(sum.toString()); // Display the result in double System.out.println("Using double, Sum is " + d); } } Output: The sum of 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473 and 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234587327189371829748971463784817489127847281478217487124872184787483274637564756745781641263981283972189743897432865743657645762376914689217817436247362472164761247612748612746 is 2469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469141651513734262516435190263143967454631918743000751861146858652219747883919392209327966909307740297653290433886520376204000415840169342671082000882825735618025902245247352219 Using double, Sum is Infinity As from above output it is clear that using double for bigger integer numbers is not a good choice. Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#add(java.math.BigInteger) Comment More infoAdvertise with us Next Article BigInteger add() Method in Java with Examples R Rajnis09 Follow Improve Article Tags : Java Java-Functions Java-BigInteger Java-math-package Practice Tags : JavaJava-BigInteger Similar Reads BigDecimal add() Method in Java with Examples The java.math.BigDecimal.add(BigDecimal val) is used to calculate the Arithmetic sum of two BigDecimals. This method is used to find arithmetic addition of large numbers of range much greater than the range of largest data type double of Java without compromising with the precision of the result. Th 3 min read AtomicInteger addAndGet() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.addandget() is an inbuilt method in java which adds the value which is passed in the parameter of the function to the previous value and returns the new updated value which is of data-type int. Syntax: public final int addAndGet(int val) Parameters: The 2 min read DoubleAdder add() method in Java with Examples The java.DoubleAdder.add() is an inbuilt method in java that adds the given value to the previous or initial value. When the object of the class is created its initial value is zero. Syntax: public void add(double x) Parameters: This method accepts a single parameter x that specifies the value to be 1 min read BlockingDeque add() method in Java with Examples The add(E e) method of BlockingDeque inserts the element passed in the parameter to the end of the Deque is there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. It works exactly in the same way as addLast() method does. 2 min read Deque addFirst() method in Java with Examples The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: 4 min read Calendar add() Method in Java with Examples The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the 3 min read CompositeName add() method in Java with Examples The add() method of a javax.naming.CompositeName class is used to add the component to the CompositeName object.There are two different add method. 1. add(int, String): This method is used to add a single component at a specified position posn passed as a parameter within this composite name. The ot 3 min read Abstract Method in Java with Examples In Java, Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type modifier. Abstraction can be achieved using abstract class and abstract methods. In this article, we will learn about Java Abstract Method. Java Abstract MethodThe abstra 6 min read AtomicIntegerArray addAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.addAndGet() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicIntegerArray. This method takes the index value and the value to be added as the parameters and returns the updated value at this i 2 min read List add(E ele) method in Java with Examples The add(E ele) method of List interface in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Parameter: This method accepts a single parameter ele which represents the element to be inserted at the end of this list. Return Value: The funct 2 min read Like