The upper bound is restricted by how the primitive long and it's wrapper class long represents that in Java it's just 64 bits. Put it this way, Long values can hold both positive and negative values. 63 bits for negative numbers spectrum, and another 63 bits for the value range of 0 and positive values. Notice that the lowest negative value is minus 2 raised to the power of 63. While 0 is involve in positive values' spectrum, understand that 2 raised to the power of 63 minus 1 is the maximum value for Long values. What we just did is we gave a consideration for the number 0.
If you calculate the answer of 2 raised to the power of 63, you're gonna end up with 9,223,372,036,854,775,808. This is an even number because 2 is also an even number. And when you when you diminish one from 9,223,372,036,854,775,808, that will bring you the maximum value for long values which is 9,223,372,036,854,775,807 .
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; ... }When we have this code, we are saving our time and effort to remember the value 9,223,372,036,854,775,807. We can just write the constant Long.MAX_VALUE in order to get the maximum Long value in Java. Check out the written example code for it's usage:
/** * This is a simple application that looks up the value of Long.MAX_VALUE and shows it to the user. **/ public class TestLong { public static void main(String[] args) { System.out.println("The maximum Long value is: "+Long.MAX_VALUE); } }This is the result of the code written above:
The maximum Long value is: 9223372036854775807Say we want to add one value to the upper limit for long values,
/** * This is a simple application that adds the value 1 to the maximum long value: */ public class TestLong { 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); } }We will get the lowest negative number that a long can represent.
The maximum Long value plus one is: -9223372036854775808.