Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how do I store large numbers in an array?

Tags:

java

arrays

In Java, how do I store numbers in an array, I mean really long numbers like up to 1 trillion so I can access them and print out in words what they are?

like image 880
Armando Moncada Avatar asked Dec 13 '22 05:12

Armando Moncada


2 Answers

1 trillion isn't that big - just use a long, which can store a number as large as 9223372036854775807 (more than a quintillion):

long[] numbers = new long[1000];

To store arbitrarily large numbers, use BigInteger, but they can be a hassle:

BigInteger[] numbers = new BigInteger[1000];
like image 140
Bohemian Avatar answered Dec 24 '22 23:12

Bohemian


You need BigInteger if its really that large. For trillion. Long is enough

like image 39
Shashank Kadne Avatar answered Dec 24 '22 21:12

Shashank Kadne