“Long” is a wrapper class in Java that stores the primitive long data type. A long can store a 64-bit two’s complement integer. It has a default value of 0L and a size of 8 bytes. It is utilized when a larger range integer value is required. The “Long.MAX_VALUE” is the static constant of the Java wrapper class Long. Its value is set as 9,223,372,036,854,775,807.
This write-up will guide you on how to use a Long.MAX_VALUE in Java.
How to Use Long.MAX_VALUE in Java?
The Long.MAX_VALUE is the static variable that contains a constant value in the Java wrapper Long class, and 9,223,372,036,854,775,807 is considered a maximum value of a long variable.
Example 1: Print Long.MAX_VALUE in Java
In this example, we will print the pre-defined maximum value of a long variable using the “System.out.println()” method:
Here, the “MAX_VALUE” is called with the class name “Long” because it is the static variable that stores a constant value of long type integer:
The output below shows the value of “Long.MAX_VALUE” as “9,223,372,036,854,775,807”:
Example 2: Adding a Number Directly in Long.MAX_VALUE
If you want to add some number in Long.MAX_VALUE, use the “+” Java operator to concatenate the specified number with the exact value of Long.MAX_VALUE; as Java does not permit you to add a number directly in it.
Here, first, we will print the original maximum value of long using “Long.MAX_VALUE”:
Then, we will add “500” to it using the “+” operator, which will simply concatenate it at the end of the value:
Output
Example 3: Adding a Number in Long.MAX_VALUE Using long Variable
In the above example, when we have tried to add a number in a Long.MAX_VALUE, it gets concatenated. Now, we will add the number to the value and print it by storing it in a variable.
We will show you what happens when a long value exceeds the Long.MAX_VALUE. To do so, create a long-type variable “newLong” to store a value, and then add the number “5” to the Long.MAX_VALUE:
Print the updated value on the console:
The output shows that the long value became negative because the variable will not store a value that exceeds the limit, and it caused memory overflow:
Example 4: Comparing User-defined long Variable Values with Long.MAX_VALUE
Here, we will check what happens when a too small or large value is compared with the Long.MAX_VALUE.
First, we will ask the user to enter any value:
We will use a “Scanner” object to get the value from the user:
Then, create a variable “value” of Long type to store the value returned by the given method:
Here, we will check whether the user entered value is greater than or less than the value of Long.MAX_VALUE using the conditional statements:
System.out.println("The entered value is too short than Long.MAX_VALUE");
}else
System.out.println("The entered value is too long than Long.MAX_VALUE");
The user entered the value “123”, which is too short according to the range of Long.MAX_VALUE; as a result, the program will print the statement “The entered value is too short than Long.MAX_VALUE” on console:
Also, specifying a value that exceeds the limit of Long.MAX_VALUE will throw an exception:
We have gathered all the relevant instructions related to using the Long.MAX_VALUE in Java.
Conclusion
The “Long.MAX_VALUE” is the static constant of the Java wrapper class Long. Its value is 9,223,372,036,854,775,807. If you want to add some numbers and save them in a variable, it will return a negative number due to memory overflow because the variable will not store a value that exceeds the limit. In this write-up, we have demonstrated the Long.MAX_VALUE in detail.