In Java, how to store large numbers in an array?

In Java, how can I store numbers in an array, I mean very long digits, for example up to 1 trillion, so I can access them and print in words what they are?

+4
source share
2 answers

You need BigInteger if it's really that big. For a trillion. Long enough

+2
source

1 trillion is not that big - just use long , which can store a number in size 9223372036854775807 (more than a quintillion):

 long[] numbers = new long[1000]; 

To save arbitrarily large numbers, use BigInteger , but they can be tricky:

 BigInteger[] numbers = new BigInteger[1000]; 
+10
source

Source: https://habr.com/ru/post/1398545/


All Articles