That is not a problem .It is the output. 1e-7 refers to 1x(10^-7) or one multiplied by ten power minus seven .

Answer from Rishabh Maurya on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › overflow and underflow in java
Overflow and Underflow in Java | Baeldung
January 8, 2024 - Simply put, overflow and underflow happen when we assign a value that is out of range of the declared data type of the variable. If the (absolute) value is too big, we call it overflow, if the value is too small, we call it underflow.
Top answer
1 of 5
225

Add a capital L to the end:

long value = 9223372036854775807L;

Otherwise, the compiler will try to parse the literal as an int, hence the error message

2 of 5
59

I don't know why it is referring to the long data type as an int

It is not. You should learn to trust compiler messages (especially when they are from sane, modern compilers and not ancient compilers that tended to have bad error messages). While the language that they speak might be hard to decipher at times, they are not usually lying to you.

Let's look at it again:

The literal of int 9223372036854775807 is out of range.

Note, that it doesn't mention your variable testLong or the type long anywhere, so the problem is not about the initialization. It seems to occur at some other point.

Now lets investigate some of the parts of the message:

  • int tells us that he wants to treat something as an int value (which is not what you wanted!)
  • "out of range" is pretty clear: something is not within the expected range (probably that of int)
  • "The literal": now that's interesting: what is a literal?

I'll leave the cozy list to talk about literals for a moment: literals are places where you have some value in your code. There are String literals, int literals, class literals and so on. Every time you mention a value explicitly in your code, it's a literal.

So it's not actually nagging you about the variable declaration, but the number itself, the value is what it's nagging you about.

You can easily verify this by using the same literal in a context where a long and an int are equally acceptable:

System.out.println(9223372036854775807);

PrintStream.println can take either an int or a long (or pretty much anything else). So that code should be fine, right?

No. Well, maybe it should be, but according to the rules it is not fine.

The problem is that "some digits" is defined to be an int literal and therefore must be in the range defined by int.

If you want to write a long literal, then you must make that explicit by appending the L (or lower case l, but I highly suggest you always use the upper-case variant, because it's much easier to read and harder to mistake for a 1).

Note that a similar problem occurs with float (postfix F/f) and double (postfix D/d).

Side note: you'll realize that there are no byte or short literals and you can still assign values (usually int literals) to byte and short variables: that's possible due to special rules in § 5.2 about conversions in an Assignment Contexts: they allow assignment of constant expressions of a larger type to byte, short, char or int if the values are within the types range.

🌐
Coderanch
coderanch.com › t › 368695 › java › Range-Data-Type-double
Range of Data Type double (Java in General forum at Coderanch)
Senthamizh To make it a long you need to put an L at the end of the assignment statement: long l = 9223372036854775807L; that should take care of it for you. Without the L the compiler thought it was an int. hope that helps ------------------ Dave Sun Certified Programmer for the Java� 2 Platform · Dave · reply reply · Bookmark Topic Watch Topic · New Topic · Boost this thread! Similar Threads · Integer Ranges again... long Vs float/double? If Statement Limitation? Widening · 'java.sql.SQLException: Out of range value for column' error ·
🌐
Coderanch
coderanch.com › t › 583184 › java › literal-type-int-range
The literal 600851475143 of type int is out of range [Solved] (Beginning Java forum at Coderanch)
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... Long literalis in Java must end with L, so 300425737572L. ETA: ninja'd. It's what Fred said. ... Boost this thread! ... Cannot convert double to long..
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
The precision of a double in Java is 10-324 decimal places, although true mathematical precision can suffer due to issues with binary arithmetic. To simplify display, you can use %d printf notation to format a Java double’s precision to two decimal places, while internally the double variable’s precision is maintained. The Double wrapper class provides two properties that represent the range of a Java double:
Find elsewhere
Top answer
1 of 6
20

Yes, I believe you should throw an exception, to help your fellow programmers notice the error at compile-time, by also adding throws ArgumentOutOfBoundsException in your method declaration.

That is, unless you use imaginary numbers in your project, where -1 is a perfectly valid argument for your square root method. :)

2 of 6
15

If you can code your method or its signature in such a way that the exception condition cannot happen, doing so is arguably the best approach.

For example, with the particular example you bring up, does your language have an unsigned integer data type with an appropriate range? If so, just use that one rather than a corresponding signed integer type and throwing an exception for any value passed in that is less than 0. In that case, a negative value cannot be used in that position in a well-formed program; the compiler will catch the mistake long before runtime, and at the very least issue a warning (for signed/unsigned type use mismatch), unless you go out of your way to explicitly bypass that safety net; and if you're forcing a signed variable into a function whose method signature says unsigned integer only without making sure that the value is within an acceptable range for that function, then I'd argue that the programmer doing so deserves to get an exception thrown in their face.

As another example, if only a reasonably small set of values is valid as input, maybe those can be expressed as an enumeration with a specific set of values rather than using a plain integer value to encode each possible value? Even if under the hood the enum collapses to an int, using enumerations protects from programmer mistakes.

If you cannot write the method signature in such a way that the exception condition cannot possibly happen in a well-formed program, then I would argue that this is exactly what ArgumentOutOfRangeException, IllegalArgumentException and similar exception types are intended for. This can also trivially be worked into a TDD workflow. Just make sure to make it clear what the error is; particularly if you don't have easy access to the source code, getting a non-specific ArgumentOutOfRangeException thrown back at you can be greatly frustrating. Note the documentation for these exception types:

ArgumentOutOfRangeException (.NET): "The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method." (as seen here)

IllegalArgumentException (Java): "Thrown to indicate that a method has been passed an illegal or inappropriate argument." (as seen here)

Writing the method to make the exception condition impossible to begin with will help even more than throwing an exception at runtime, but we must acknowledge that sometimes this is just not possible in practice. You'll still need to check invariants not directly related to the range of a single parameter value at runtime, though; for example, if x + y > 20 is an error, but x and y each can be anything <= 20, I don't think there's an easy way to express that in the method signature.

🌐
Baeldung
baeldung.com › home › java › java numbers › fixing the “java: integer number too large” error
Fixing the “java: integer number too large” Error | Baeldung
February 20, 2025 - Java stores Integer using 32 bits of memory. Thus, Integer‘s(or int‘s) range is from -231 (-2,147,483,648) to 231-1 (2,147,483,647).
🌐
Programming.Guide
programming.guide › java › double-range.html
Java: Range of a double | Programming.Guide
In Java, a double is a 64-bit IEEE 754 floating point. Double values are symmetrical around origo and has a maximum magnitude of 1.7976931348623157e308
🌐
javaspring
javaspring.net › blog › java-double-to-long
Java Double to Long: A Comprehensive Guide — javaspring.net
Here is an example of declaring a long variable: ... Converting a double to a long involves losing the fractional part of the double value. Also, if the double value is outside the range of the long data type, the result will be undefined.
🌐
Wikipedia
en.wikipedia.org › wiki › Integer_overflow
Integer overflow - Wikipedia
2 weeks ago - For an unsigned type, when the ideal result of an operation is outside the type's representable range and the returned result is obtained by wrapping, then this event is commonly defined as an overflow.
🌐
Sololearn
sololearn.com › en › Discuss › 233085 › in-java-is-there-any-data-type-having-range-more-than-the-range-of-the-primitive-data-type-double-to-store-no
In java, is there any data type having range more than the range of the primitive data type "double" to store no.? | Sololearn: Learn to code for FREE!
... double is the "biggest" primitive type, you'd have to use the BigDecimal class for larger numbers and more precision. Working with BigDecimal is a bit more complicated, since you cannot use +, -,.. operators directly.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Managing non-value doubles in Java - Java Code Geeks
October 10, 2025 - This article provides a comprehensive overview of how to identify and handle “non-value” doubles in Java—values like NaN, Infinity, and those falling outside of logical numeric boundaries. It highlights why such values occur, the risks they pose to program stability, and several techniques to manage them effectively, including throwing exceptions, filtering invalid data, applying range constraints, using Optional<Double> wrappers, and employing Double.NaN for undefined results.