Long.MAX_VALUE & Long.MIN_VALUE in Java [Practical Examples]


Written by - Deepak Prasad

Java Long.MAX_VALUE and Long.MIN_VALUE

Introduction

In Java, long is a primitive type that stores integers but gives a wider range of values. The long data type is signed that occupies 64 bit (8 bytes) of memory. That means, it can store values in the range of -2^63 to 2^63 -1. In other words it can store values in the range of -9,223,372,036,854,775,807 and 9,223,372,036,854,775,808.

However, it is a tough task to remember the exact value of such a huge number. Thus, in Java, we have the Long class in java.lang Package that wraps a value of the primitive type long in an object. It has constants MAX_VALUE and MIN_VALUE for representing these huge numbers. In other words, the MAX_VALUE is a constant holding the maximum value a long can have, 2^63 -1. Whereas, the MIN_VALUE is a constant holding the minimum value a long can have,-2^63. However, when we try to store value greater than MAX_VALUE and value less than MIN_VALUE it leads to memory underflow or memory overflow resulting in unexpected values.

 

Syntax

The statement below shows the syntax of using constants MAX_VALUE and MIN_VALUE.

// Syntax for MAX_VALUE constant
Long.MAX_VALUE;

// Syntax for MIN_VALUE constant
Long.MIN_VALUE;

 

Some Practical Examples

Example 1: Display value of constants Long.MAX_VALUE and Long.MIN_VALUE

In this example, we will display the value of constants Long.MAX_VALUE and Long.MIN_VALUE in a Long class. We get max value

// Program to demonstrate the use of Long.MAX_VALUE and Long.MIN_VALUE constants of Long class
public class Main {
    public static void main(String[] args) {
        // Printing the maximum and minimum value allowed in long variable
        // Max value is equal to 2^63 - 1
        System.out.println("The maximum value allowed in long variable is : " + Long.MAX_VALUE);

        // Min value is equal to -2^63 
        System.out.println("The minimum value allowed in long variable is : " + Long.MIN_VALUE);
    }
}

Output

The maximum value allowed in long variable is : 9223372036854775807
The minimum value allowed in long variable is : -9223372036854775808

 

Example 2: Initializing variable with Long.MAX_VALUE+1

In this example, we will initialize the long variable with the value of constants Long.MAX_VALUE+1. As we can see, adding a 1 to this constant prints a negative number as no variable could store any value beyond this maximum limit. Thereby leading to memory overflow.

// Program to demonstrate the use of MAX_VALUE constant of Long class
public class Main {
    public static void main(String[] args) {
        // Initializing variables
        long l1 = Long.MAX_VALUE;
        long l2 = Long.MAX_VALUE + 1;
        // Printing the value of variable after initialization
        System.out.println("The variable l1 is initialized with MAX_VALUE : " + l1);
        System.out.println("The variable l2 is initialized with MAX_VALUE + 1 : " + l2);
    }
}

Output

The variable l1 is initialized	with MAX_VALUE : 9223372036854775807
The variable l2 is initialized with MAX_VALUE + 1 : -9223372036854775808

 

Example 3: Checking for valid account number in Bank Class

Here, we will get name and 19 digit account number as an input from the user. Thereafter,  we will use the constant MAX_VALUE to check if the given input of account number is valid or not in a Bank class.  That means, if the user gives 20 digit input, it will raise an exception.


// Program to demonstrate the use of MAX_VALUE constants of Long class
// Imporing package for Scanner 
import java.util.*;
public class Bank {
    public static void main(String[] args) {
        // Creating scanner object to get user input
        Scanner sc = new Scanner(System.in);

        // Get name and account number as input from the user
        System.out.println("Enter your Name");
        String name = sc.nextLine();
        long accno = 1;

        // Check if user input account numbber is valid account number or not
        // Using try catch to handle the exception
        try {
            System.out.println("Enter your 19 digit account number");

            // If the user input is greater than max value it will throw an exception
            if (sc.nextLong() & gt; Long.MAX_VALUE)
                throw new Exception("Invalid");
        } catch (Exception e) {
            System.out.println("Invalid account number");
        }
    }
}

Output

Enter your Name
Sweety
Enter your 19 digit account number
11223344556677889911
Invalid account number

 

Example 4: Initializing variable with Long.MIN_VALUE - 1

In this example, we will initialize the long variable with the value of constants Long.MIN_VALUE - 1. As we can see, subtracting a 1 from this constant prints a positive number as no variable could store any value beyond this minimum limit. Thereby leading to memory underflow.


// Program to demonstrate the use of MIN_VALUE constant of Long class
public class Main {
    public static void main(String[] args) {
        // Initializing variables
        long l1 = Long.MIN_VALUE;
        long l2 = Long.MIN_VALUE - 1;

        // Printing its values
        System.out.println("The variable l1 is initialized with MIN_VALUE : " + l1);
        System.out.println("The variable l2 is initialized with MIN_VALUE - 1 : " + l2);
    }
}

Output

The variable l1 is initialized 	with MIN_VALUE : -9223372036854775808
The variable l2 is initialized with MIN_VALUE - 1 : 9223372036854775807

 

Example 5: Demonstrating Memory Overflow.

Here, We are computing the multiplication of three variables and check if the result of the expression goes out of range of long value. We will use the constant MAX_VALUE to check if the result of expression leads to memory overflow or not.


// Program to demonstrate the use of MAX_VALUE constant of Long class
public class Main {
    public static void main(String[] args) {
        // Initializing variables
        long a = 12345;
        long b = 34567;
        long c = 432551;

        // Check if the result does not lead to memory overflow
        try {
            if ((a * b * Math.pow(c, 999)) & gt; Long.MAX_VALUE)
                throw new Exception("Invalid");
        } catch (Exception e) {
            System.out.println("Memory Overflow");
        }
    }
}

Output


Memory Overflow

 

Example 6: Demonstrating Memory Underflow.

Here, We are computing the multiplication of three variables and check if the result of the expression goes out of range of long value. We will use the constant MIN_VALUE to check if the result of expression leads to memory overflow or not.


// Program to demonstrate the use of MIN_VALUE constant of Long class
public class Main {
    public static void main(String[] args) {
        // Initializing
        long a = 12345;
        long b = 34567;
        long c = -432551;

        // Check if the result does not lead to memory underflow
        try {
            if ((a * b * Math.pow(c, 999)) & lt; Long.MIN_VALUE)
                throw new Exception("Invalid");
        } catch (Exception e) {
            System.out.println("Memory Underflow");
        }
    }
}

Output


Memory Underflow

 

Summary

The knowledge of Long.MAX_VALUE and Long.MIN_VALUE is very useful in Java. In many applications, we may need to prevent underflow and overflow of the memory. In this case, we can use this constants to prevent failure of the program and take an appropriate action if value goes out of range.

In this tutorial, we covered everything about the MAX_VALUE and MIN_VALUE constants of Long class of java.lang package with example.

 

References

Long class

 

Views: 20

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment