Long.MAX_VALUE in Java

Different data types have different ranges in programming. The long data types are usually used when we require a more extensive range, which ultimately results in a larger size in memory. We may encounter the need to assign variables with the maximum value that it can hold. But it is a difficult job to remember such big numbers with the exact value. So in Java, we have constants for representing these huge numbers. We will be discussing the Long.MAX_VALUE value in this tutorial.

The long integer data type is a 64 bit signed two’s complement integer. The maximum value of long is 9,223,372,036,854,775,807. The Long.MAX_VALUE is a constant from the java.lang package used to store the maximum possible value for any long variable in Java.

In the code below, we will print this Long.MAX_VALUE constant.

public class Long_Max_value{  
    public static void main(String[] arg)
    {
        System.out.println("Long.MAX_VALUE = "
                           + Long.MAX_VALUE);
    }
}

Output:

Long.MAX_VALUE = 9223372036854775807

Adding a 1 to this constant will print a negative number as no variable could store any value beyond this maximum limit. Doing so will overflow the memory.

See the following example.

public class Long_Max_value {
    public static void main(String[] arg)
    {
  
        try {
  
            System.out.println("Long.MAX_VALUE + 1");
            Long N = Long.MAX_VALUE + 1;
            System.out.println(N);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Long.MAX_VALUE + 1
-9223372036854775808
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Integer

  • Convert Int to Float in Java
  • Difference Between Long and Int Data Types in Java