Large numbers
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
The integer primitive type with the largest range of value is the long
, from -263 to 263-1. If you need greater or lesser values, you have to use the BigInteger
class in the package java.math
. A BigInteger
object can represent any integer (as large as the RAM on the computer can hold) as it is not mapped on a primitive type. Respectively, you need to use the BigDecimal
class for great decimal numbers.
However, as these perform much slower than primitive types, it is recommended to use primitive types when it is possible.
BigInteger
[edit | edit source ]The BigInteger
class represents integers of almost any size. As with other objects, they need to be constructed. Unlike regular numbers, the BigInteger
represents an immutable object - methods in use by the BigInteger
class will return a new copy of a BigInteger
.
To instantiate a BigInteger
, you can create it from either byte array, or from a string. For example:
BigIntegeri=newBigInteger("1000000000000000000");
BigInteger
cannot use the normal Java operators. They use the methods provided by the class.
BigIntegera=newBigInteger("3"); BigIntegerb=newBigInteger("4"); // c = a^2 + b^2 BigIntegerc=a.multiply(a).add(b.multiply(b));
It is possible to convert to a long
, but the long
may not be large enough.
BigIntegeraBigInteger=newBigInteger("3"); longaLong=aBigInteger.longValue();
BigDecimal
[edit | edit source ]The BigInteger
class cannot handle decimal numbers. The BigDecimal
class represents a floating point value of arbitrary precision. It is composed of both a BigInteger
, and a scale value (represented by a 32-bit integer).
To do:
Add some exercises like the ones in Variables