[Solved] Error: integer number too large

In this short post, I will be sharing how to fix "integer number too large". This error is a compile-time error in Java. This error occurs when the java compiler tries to interpret the given number as a constant value of type int by default. As always, first, we will produce the "integer number too large" error before moving on to the solution.

Read Also: [Fixed] int cannot be converted to int[]

[Fixed]: Error: Integer number too large

Example 1: Producing the error by assigning an int to a long data type


We can easily produce this error by assigning an int to a variable whose data type is long as shown below:

public class IntegerNumberTooLarge {
    public static void main(String args[]) {
      
long x = 123456789101112;
System.out.println("variable x value is: " + x); } }


Output:
IntegerNumberTooLarge.java:3: error: integer number too large
           long x = 123456789101112;
                          ^
1 error



Explanation:

The cause of this error is by assigning an int to a variable whose data type is long. Integer literals are by default int in Java. This causes an error since 123456789101112 literal can not be represented with an int.

Solution:

The solution to this error is to append "l" or "L" after the number. Use "L" instead of "l" because lowercase "l" makes it hard to distinguish with "1", as a result, you should always use the uppercase "L". In Java, the compiler can interpret the long data type as long as the number to be interpreted contains 'l' or 'L' after it. It can be represented in the code as shown below:

public class IntegerNumberTooLarge {
    public static void main(String args[]) {
      
long x = 123456789101112L;
System.out.println("variable x value is: " + x); } }


Output:
variable x value is: 123456789101112


That's all for today, please mention in the comments in case you are still facing the error java integer number too large.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry