Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Long primitive type maximum limit [duplicate]

Tags:

java

People also ask

What is the maximum limit of long in Java?

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

Is there anything bigger than long in Java?

The largest integer number that a long type can represent is 9223372036854775807. If we deal with even larger numbers, we have to use the java. math. BigInteger class.

How do you store numbers bigger than long?

Use BigInteger if you work with a long and use BigDecimal if you work with floatingpoint numbers. The BigInteger can be as big as you want, till there is not enough RAM. But have to use the BigInteger methods to do calculations and in the example you see that BigInteger is immutable.


Long.MAX_VALUE is 9,223,372,036,854,775,807.

If you were executing your function once per nanosecond, it would still take over 292 years to encounter this situation according to this source.

When that happens, it'll just wrap around to Long.MIN_VALUE, or -9,223,372,036,854,775,808 as others have said.


It will overflow and wrap around to Long.MIN_VALUE.

Its not too likely though. Even if you increment 1,000,000 times per second it will take about 300,000 years to overflow.


Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

It will start from -9,223,372,036,854,775,808

Long.MIN_VALUE.

Exceding the maximum value of a long doesnt throw an exception, instead it cicles back. If you do this:

Long.MAX_VALUE + 1

you will notice that the result is the equivalent to Long.MIN_VALUE.

From here: java number exceeds long.max_value - how to detect?