1 2 Previous Next 18 Replies Latest reply: Mar 19, 2009 4:16 AM by 800305 RSS

    What is BigInteger?

    843785
      I tried reading the API and googling but couldn't really find anything that tell me what it is or I'm just not understanding it. So what's the difference between an int and a BigInteger? How many bits is a BigInteger? The closest thing I could find was "a BigInteger allows you to store an arbitrary big non-decimal number" . Can anyone please clarify BigInteger for me? Thanks.
        • 1. Re: What is BigInteger?
          843785
          int is a primitive. BigInteger is a class.
          • 2. Re: What is BigInteger?
            843785
            It's a class that allows you to represent an integer of pretty much any size you want. Yep, even really big ones.
            • 3. Re: What is BigInteger?
              577611
              The biggest primitive integer in Java is the long. A long has a size of 64 bits. The range of long is -9223372036854775808 to 9223372036854775807.

              What if you wanted to multiply this number 40923849028209382309482340982340823408230942409234 with 24820482340823408234098230394. Long can't help you.

              You would do this with BigInteger.

              BTW, the product of the numbers above is 1015749672123194169695635328377767879846266879589364537406578021548606365058196.

              Edited by: rksharma on 19 Mar, 2009 2:50 PM
              • 4. Re: What is BigInteger?
                800305
                bigintiger is a class,
                here, read this : http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html
                • 5. Re: What is BigInteger?
                  r035198x
                  rksharma wrote:
                  The biggest primitive integer in Java is the long. A long has a size of 64 bits. The range of long is -9223372036854775808 to ..
                  You may (or may not) be thrilled to know that
                  long a = -(9223372036854775808l);
                  does not compile.
                  • 6. Re: What is BigInteger?
                    577611
                    r035198x wrote:
                    The biggest primitive integer in Java is the long. A long has a size of 64 bits. The range of long is -9223372036854775808 to ..
                    Took it out of here. [http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html]

                    However, this compiles
                    long a = -(9223372036854775807l);
                    • 7. Re: What is BigInteger?
                      800308
                      This compiles and runs.
                      package forums;
                      
                      class MinLongTesterator
                      {
                        public static void main(String[] args) {
                          try {
                            long l = -9223372036854775808L;
                            System.out.println(l);
                          } catch (Exception e) {
                            e.printStackTrace();
                          }
                        }
                      }
                      • 8. Re: What is BigInteger?
                        r035198x
                        >
                        However, this compiles
                        long a = -(9223372036854775807l);
                        So should the example I posted above.
                        Its semantic equivalent
                        long a = -9223372036854775808l;
                        compiles just fine.
                        • 9. Re: What is BigInteger?
                          577611
                          The java language specification says that a compile time error occurs if 9223372036854775808L appears anywhere without the "-" opertor.
                          • 10. Re: What is BigInteger?
                            r035198x
                            rksharma wrote:
                            The java language specification says that a compile time error occurs if 9223372036854775808L appears anywhere without the "-" opertor.
                            Indeed.
                            And therefore
                            long a = -(-9223372036854775808l);
                            compiles(!) while
                            long a = 9223372036854775808l;
                            does not.
                            • 11. Re: What is BigInteger?
                              577611
                              The compiler, does not treat the following two statements as the same.
                              long a = -9223372036854775808l;
                              and
                              long a = -(9223372036854775808l);
                              In the second case, 9223372036854775808l appears without the unary "-" operator.
                              • 12. Re: What is BigInteger?
                                r035198x
                                Check my post above again. There are two unary - in there.

                                Also see the output of
                                long a = -(-9223372036854775808l);
                                System.out.println(a);
                                
                                long b = -(-9223372036854775807l);
                                System.out.println(b); 
                                Terrible business if you ask me. None of this happens for the minimum values of other primitives of course.
                                • 13. Re: What is BigInteger?
                                  577611
                                  long a = -(-9223372036854775808l);
                                  System.out.println(a);
                                  gives
                                  -9223372036854775808
                                  Do you think it has something to do with this.
                                  long l = Long.MAX_VALUE;
                                  System.out.println(l);
                                  l++;
                                  System.out.println(l);
                                  gives
                                  9223372036854775807
                                  -9223372036854775808
                                  • 14. Re: What is BigInteger?
                                    r035198x
                                    rksharma wrote:

                                    Do you think it has something to do with this.
                                    ....
                                    No. That one is just a well behaved snippet exhibiting the expected behavior.
                                    1 2 Previous Next