Riddick51PB
Riddick51PB

Reputation: 41

How to assign very large numbers to variable in java?

I'm getting "integer number too large: 1000000000001" for the following code line. How do I make it so that maxValue can hold 1 quadrillion or 1 trillion?

long maxValue = 1000000000001;      //1,000,000,000,001

Upvotes: 4

Views: 9724

Answers (3)

Alireza Noorali
Alireza Noorali

Reputation: 3265

This answer is complete.

Java Language Specification:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234807

You need to use a long literal (with an L at the end):

long maxValue = 1000000000001L; //1,000,000,000,001

Note that you don't need to use BigInteger if your numbers are between -263 and 263-1 (inclusive). (263-1 = 9223372036854775807L = 0x7fffffffffffffffL.)

Upvotes: 10

Kristiono Setyadi
Kristiono Setyadi

Reputation: 5643

You may check for another question and answer before you ask. Someone probably has the same question and got the best answer. See this:

Large Numbers in Java

This is probably what you need.

Upvotes: 0

Related Questions