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.