Long Data Type In Java

Long Data Type In Java | The Long is one of eight primitive data types in Java, it is also called the fundamental data type. The long data type is the type of integer that stores only the numeric values but not the floating point numbers containing the decimal value.

The long can occupy the largest memory space up to 64 bits, that is it ranges from -9223372036854775808 to 9223372036854775807 i.e. -263 to 263-1. So we can say that to store any large numeric value we can use a long data type. To find the minimum and maximum value of the long data type Java Long class provides the constant that stores the minimum and maximum values of the long, they are MIN_VALUE and MAX_VALUE respectively.

public class Main {
    public static void main(String[] args) {
        System.out.println("Long min Value: " + Long.MIN_VALUE);
        System.out.println("Long max Value: " + Long.MAX_VALUE);


Long min Value: -9223372036854775808
Long max Value: 9223372036854775807

While working with data types we need to know the range of each data type in order to avoid errors, knowing all the minimum and maximum values of each data type is quite tedious hence to overcome that Java has made it easy by providing these constants. As said above the minimum value of long is -9223372036854775808 and the maximum value is 9223372036854775807 and the default value is 0.

Size :: 8 bytes
Range ::  -263 to 263-1
          to 9223372036854775807
Default value :: 0
Corresponding Wrapper class :: Long

Representation of Long Data Type In Java

To represent an int data type value as a long data type we must write one of the below code:-

long n3 = 9; // variable assignment

(long)9; // casting
// it will convert 9 as int to long

9L or 9l  // using suffix L or l

While declaring the long values we should append ‘L’ or ‘l’ (capital or small L) at the end indicating it is a long value. Below is an example of the long data type in Java.

public class Main {
    public static void main(String[] args) {
        long number = 8888888888888L;
        System.out.println("Number: " + number);


Number: 8888888888888

By default, any number declared in Java is treated as of int data type. if the given number belongs to the int data type range then we can assign it to a long data type without any suffix. Due to auto-promotion, the int value is internally converted as long by JVM.

int n1 = 123; // valid
long n2 = 123; // valid

The number 8888888888888 is out of the int data type range but belongs to the long data type range. To represent it as a long data type we must add the suffix ‘l’ or ‘L’ to it.

int n1 = 8888888888888; // invalid
long n2 = 8888888888888; // invalid
long n3 = 8888888888888l; // valid
long n4 = 8888888888888L; // valid

Note:- Lowercase l (small L) looks like 1 (one) and sometimes it creates confusion, so it is suggested to use the capital letter (L) instead of the small letter (l) for the long data type.

How To Use Long Data Type In Java

The default size of the long data type is 8 bytes and the default value is 0L. The below code demonstrates the use of long data types for both positive and negative values.

public class Main {
    public static void main(String[] arg) {
        long number = 8888812487635898L;
        long number1 = -3698745123654789L;
        System.out.println("Positive long number: " + number);
        System.out.println("Negative long number: " + number1);


Positive long number: 8888812487635898
Negative long number: -3698745123654789

public class Main {
    public static void main(String[] arg) {
        long num1 = 8888812487635898L;
        long num2 = -3698745123654789L;
        System.out.println("Sum: " + (num1 + num2));
        System.out.println("Subtraction: " + (num1 - num2));
        System.out.println("Multiplication: " + (num1 * num2));
        System.out.println("Division: " + (num1 / num2));
        System.out.println("Remainder: " + (num1 % num2));


Sum: 5190067363981109
Subtraction: 12587557611290687
Multiplication: 5279030630842837086
Division: -2
Remainder: 1491322240326320

Error: Integer Number Too Large

By default, large integer numbers also consider as int type by the Compiler. But large integers are not in the range of int data type. Hence when we use a large integer number directly, the compiler will throw an error. The compiler throwing error is “integer number is too large

// error: integer number too large

The compiler considers 987654321012345 as an integer number but this number exceeds the range of the int data type, we got an error.

To use a large integer number, we must suffix this number with L or l. Now, the compiler will treat the number as a long type. The number comes under the range of the long data type so we don’t get any errors.

// Output:- 987654321012345

The long data type has also a range and it does not support a very long number like 100! If we want to store a very very long number which is out of range of the long data type then how can we store them in Java? In that case, we use the string-referenced data type. BigDecimal and BigInteger classes are also there to represent large values. Using string we can store those very very long numbers.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *