Grails Cookbook - A collection of tutorials and examples

Maximum Value of Long in Java

In Java, the data type integer is commonly used to calculate numbers. An integer with bigger range of values is efficient for some programs. Which will bring us to bits, an integer can only hold 32 bits whereas Java Long is capable of holding 64 bits. Now come to think of it, when you're creating a program that's required an integer exceeding 32 bits, you would realize Java Long is more effective to utilize. Understand that even though Java Long has bigger capacity than an integer, it still has a boundary limit for maximum value which is 9,223,372,036,854,775,807.

Long Maximum Value is about...

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 .

Check out the 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;
...
}
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: 9223372036854775807
Say we want to add one value to the upper limit for long values,
let's find out and try it.
/**
 * 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.
Here is the output of the code:
The maximum Long value plus one is: -9223372036854775808.