 Java Long Max Value

We usually use integers when we want to calculate whole numbers. But Integers in Java can only hold 32 bit values which could be insufficient for some tasks. When we need bigger range of values, we could use long values. In Java, Long values is represented in 64 bits. However, even if this can hold bigger range of values, there is still a limit to it's maximum value. The Java Long Max Value is 9,223,372,036,854,775,807. Below are some more details to explain this.

Explanation for Long Max Value

The primitive long and it's wrapper class Long is represented by 64 bits in Java, and so the upper bound is limited by this. Since Long values can hold both negative and positive values, around 63 bits represents the negative number spectrum, while another 63 bits holds the value range of 0 and positive values. So since 0 is included in the other side of the spectrum, the maximum value Long can hold is 2 raised to the power of 63 minus 1. We need to do the minus one to account for the number 0. While the lowest negative value is minus 2 raised to the power of 63.

So 2 raised to the power of 63 is 9,223,372,036,854,775,808 which is an even number because 2 is an even number. And deducting one will bring the value 9,223,372,036,854,775,807 - which is the maximum value for long values.

As of the time of this writing, here is a code snippet from the Long class that defines the constant maximum value of Long in Java:
```public final class Long extends Number implements Comparable<Long> {
...
/**
* A constant holding the maximum value a {@code long} can
* have, 2<sup>63</sup>-1.
*/
@Native public static final long   MAX_VALUE = 0x7fffffffffffffffL;
...
}
```
Since we have this constant, we don't need to remember the value 9,223,372,036,854,775,807. We can just use the constant Long.MAX_VALUE to get the maximum Long value in Java. Here is an example code for it's usage:
```/**
* A simple application that looks up the value of Long.MAX_VALUE and outputs it to the user.
**/
public class Test {
public static void main(String[] args) {
System.out.println("The maximum Long value is: "+Long.MAX_VALUE);
}
}
```
The result of this code is this:
```The maximum Long value is: 9223372036854775807
```
Since this is already the upper limit for Long values, what will happen if we add one to it? The best way to answer is to try and code it:
```/**
* A simple application that tries to add the value 1 to the maximum long value:
*/
public class Test {
public static void main(String[] args) {
long maxLongValue = Long.MAX_VALUE;
maxLongValue = maxLongValue + 1;
System.out.println("The maximum Long value plus one is: " + maxLongValue);
}
}
```
Surprisingly, we will get the lowest negative number that a long can represent. Here is the output of the code:
```The maximum Long value plus one is: -9223372036854775808.
```