Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.

Answer from Jay on Stack Overflow
Top answer
1 of 5
27

Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.

2 of 5
3

Please run this code:

System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);

So you can see why it fails.

🌐
IBM
ibm.com › docs › en › i › 7.4.0
COBOL and Java Data Types
We cannot provide a description for this page right now
Discussions

static typing - Why don't many languages have integer range types? - Programming Language Design and Implementation Stack Exchange
As an example, Checked Exceptions ... both in Java and C++, as there are no good facilities to manipulate the checked exception list. This results in Stream.map having no exception specification, when it should transparently propagate them. This means that adding Integer Ranges in a language ... More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
What is the best way to store an integer range in java?
Just store the min and max in separate int variables. That's the easiest approach. To generate the numbers to solve all you need to do is generate a random number in the range. Here, you can use the .nextInt() method from the Random class: Random rng = new Random(); // create a new Random generator instance int numberInRange = rng.nextInt((max + 1) - min) + min; The line int numberInRange = rng.nextInt((max + 1) - min) + min; generates a random number in the range min to max (inclusive). The parameter of the .nextInt() method is the maximum range excluded, so .nextInt(10) will generate random numbers in the range 0 to 9. By adding 1 to max I include max in the range, and then I subtract the min to get the range of possible numbers, that then need to be offset by adding min. As a concrete example: your range 10 to 100 inclusive Random rng = new Random(); // create a new Random generator instance int min = 10; int max = 100; int numberInRange = rng.nextInt((max + 1) - min) + min; When you calculate: max + 1, you get 101. Were you using this value as upper boundary for the .nextInt method, you'd get numbers in the range 0 to 100 inclusive. since you want 10 to 100 inclusive, you only have 91 distinct numbers, so you need to subtract the min (10) from the above result, 101 - 10 yields 91 Now, the .nextInt method produces numbers in the range 0 to 90 inclusive Last, by adding min to the random number generated above, you offset the range so that it goes from 10 to 100. Edit: optimally, you'd move the random number in range generation into its own method. My approach would be as follows: At class level (as a field): static Random rng = new Random(); The method itself: public static int rangedRandomInt(int min, int max) { return rng.nextInt((max + 1) - min) + min; } Or, with late initialisation At class level (as a field): static Random rng; The method itself: public static int rangedRandomInt(int min, int max) { if(rng == null) { // on-demand initialization of Random number generator rng = new Random(); } return rng.nextInt((max + 1) - min) + min; } More on reddit.com
🌐 r/learnprogramming
2
0
August 22, 2017
java - How to check if an integer is in a given range? - Stack Overflow
My RangeChecker object was actually a joke, but in hindsight, it actually isn't that bad of a solution (even if it is overkill.) 2011-04-06T23:20:30.107Z+00:00 ... It would be better if it were a method directly defined in Integer class. More on stackoverflow.com
🌐 stackoverflow.com
How to calculate range and median from a list of integer numbers?
Are you successfully using Scanner to read the input? Is that input in integer form? Your post doesn't fully specify where you're getting stuck. For range, you only need to know the lowest and highest values the user has entered so far. With each value entered, you just need to check if either of these values are altered. Median is trickier to maintain as the user is continuously entering values- I'm guessing for a 100 level class, early in the semester, they're expecting you to store all of the input and at the very end calculate the median across all of the stored numbers. Are you comfortable using an ArrayList to build up a list of stored values? More on reddit.com
🌐 r/learnjava
8
1
February 1, 2017
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those ...
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › IntegerRange.html
IntegerRange (Apache Commons Lang 3.20.0 API)
Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if below, or the range maximum if above. IntegerRange range = IntegerRange.of(16, 64); range.fit(-9) --> 16 range.fit(0) --> 16 range.fit(15) --> 16 range.fit(16) --> 16 range.fit(17) --> 17 ...
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
🌐
GeeksforGeeks
geeksforgeeks.org › java › intstream-range-java
IntStream range() in Java - GeeksforGeeks
December 6, 2018 - Java Collection · Last Updated : 6 Dec, 2018 · IntStream range(int startInclusive, int endExclusive) returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1. Syntax : static ...
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › checking-if-a-number-is-within-a-range-in-java-da2729ea3e05
Checking if a Number Is Within a Range in Java | Medium
August 25, 2025 - The most common way to test a range is by chaining two comparisons together with the logical && operator. This style is clear, fast, and doesn’t call anything extra. It works for integers, floating point values, and even characters, since characters in Java are numeric values underneath.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › VariableBasics › minAndMax.html
3.7. Integer Min and Max — AP CSA Java Review - Obsolete
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Why those numbers? Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space. In 32 bits of space with one bit used to represent the sign you can represent ...
Top answer
1 of 6
14

The most popular reason is simply that having additional types is more work for the authors of compilers and libraries. Every feature starts with -100 points, and only gets implemented when there's a compelling reason to do so.

In low-level languages like C, automatic bounds-checking was deliberately avoided because it was seen as inefficient. Better to let a program crash with a segfault or divide-by-zero error than to waste precious CPU instructions validating conditions that the programmer should have already explicitly checked.

If you do implement range types, you'll have to figure out the semantics of doing arithmetic on them. So, using your example of a: 10..20 and b: 5..7, you'd need:

  • a + b: 15..27
  • a - b: 3..15
  • a * b: 50..140
  • a / b: 1..4

What happens if you define c : 1..100 and then write c = a * b;? Do you give a compile-time error? Throw an exception at runtime if a * b > 100?

Moreover, a lot of real-world values just don't have well-defined bounds. For example, if you're writing a Date class based on the Gregorian calendar, defining month: 1..12 and day: 1..31 seems obvious, but what bounds do you put on year? Do you start with 0, 1, 1601, 1753, 1900, 1970, 2000, or -3760? Do you end with 2038, 2099, 2999, or 9999? You'd have to make an arbitrary decision.

It's just easier to use plain int.

2 of 6
8

In addition to the other fine answers, I'll note that many languages outsource their underlying type system to a "lowest common denominator" type system provided by a runtime, and then layer some small additional features on top of that. The languages of the .NET runtime all have basically the same type system with minor variations; similarly for the JVM languages, and so on.

This has pros and cons for the language designer. On the pro side, if you like the type system then you save on time and effort designing one. On the con side, we spent a fair amount of time on the C# team asking "why does the verifier not like this typesafe-in-C# program?" and discovering that there was some subtle rule of the runtime's type verifier that we were violating.

But the big benefit for a platforms company like Microsoft is interoperability between languages: that you can write libraries in one language and use them in any compatible language.

That then leads to the big cost: what happens when two or more languages disagree on the design for a type not represented in the underlying type system, or represented in a way that doesn't meet all the needs of a particular language? We saw this play out between C# and F# over some decades as the designs for various kinds of tuple types evolved.

If the designers of C# wanted to add ranged integers -- and it's an entirely reasonable suggestion on the face of it -- one of the first pushbacks they'd give you is "will adding this feature to C# create work for the runtime team, the F# team, or anyone else who provides a language in this ecosystem?"

Creating work for others is a lot of points against a feature, particularly when there are potential new features that produce benefits for maintainers of other languages in the ecosystem, rather than costs.

🌐
Baeldung
baeldung.com › home › java › java numbers › how to check whether an integer exists in a range with java
How to Check Whether an Integer Exists in a Range with Java | Baeldung
January 18, 2024 - A range class that does not require importing an external library is java.time.temporal.ValueRange, introduced in JDK 1.8: public class IntRangeValueRange { public boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) { final ValueRange range = ValueRange.of(lowerBound, upperBound); return range.isValidIntValue(number); } public boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) { final ValueRange range = ValueRange.of(lowerBound + 1, upperBound - 1); return range.isValidIntValue(number); } public boolean isInOpenClosedRange(Integer number,
🌐
Quora
quora.com › Is-it-possible-to-change-the-range-of-integer-data-type-in-Java-If-yes-how-If-not-how-can-you-take-a-number-with-millions-of-digits-I-want-millions-of-digits-in-a-single-data-type
Is it possible to change the range of integer data type in Java? If yes, how? If not, how can you take a number with millions of digits? I want millions of digits in a single data type. - Quora
Answer (1 of 5): No range cannot be changed .It is fixed by default. Although you can use BigInteger class in Java to tackle with this problem. JAVA In Java BigInteger class is used to deal with very large number . The BigInteger class allocates as much memory as it needs to hold all the bits ...
🌐
Reddit
reddit.com › r/learnprogramming › what is the best way to store an integer range in java?
r/learnprogramming on Reddit: What is the best way to store an integer range in java?
August 22, 2017 -

I am making a math workout program and I need to prompt the user to input what digit range would he want in his math problem (using numbers from 0 to 9, from 10 to 100...).

I thought about making an int array (say I want addition problems that user numbers no bigger than 100 and no smaller than 0, id write int[] range = {0, 100}).

Then id use that array in generating numbers for the problem.

Is this the best way?

Top answer
1 of 2
5
Just store the min and max in separate int variables. That's the easiest approach. To generate the numbers to solve all you need to do is generate a random number in the range. Here, you can use the .nextInt() method from the Random class: Random rng = new Random(); // create a new Random generator instance int numberInRange = rng.nextInt((max + 1) - min) + min; The line int numberInRange = rng.nextInt((max + 1) - min) + min; generates a random number in the range min to max (inclusive). The parameter of the .nextInt() method is the maximum range excluded, so .nextInt(10) will generate random numbers in the range 0 to 9. By adding 1 to max I include max in the range, and then I subtract the min to get the range of possible numbers, that then need to be offset by adding min. As a concrete example: your range 10 to 100 inclusive Random rng = new Random(); // create a new Random generator instance int min = 10; int max = 100; int numberInRange = rng.nextInt((max + 1) - min) + min; When you calculate: max + 1, you get 101. Were you using this value as upper boundary for the .nextInt method, you'd get numbers in the range 0 to 100 inclusive. since you want 10 to 100 inclusive, you only have 91 distinct numbers, so you need to subtract the min (10) from the above result, 101 - 10 yields 91 Now, the .nextInt method produces numbers in the range 0 to 90 inclusive Last, by adding min to the random number generated above, you offset the range so that it goes from 10 to 100. Edit: optimally, you'd move the random number in range generation into its own method. My approach would be as follows: At class level (as a field): static Random rng = new Random(); The method itself: public static int rangedRandomInt(int min, int max) { return rng.nextInt((max + 1) - min) + min; } Or, with late initialisation At class level (as a field): static Random rng; The method itself: public static int rangedRandomInt(int min, int max) { if(rng == null) { // on-demand initialization of Random number generator rng = new Random(); } return rng.nextInt((max + 1) - min) + min; }
2 of 2
2
I agree with u/desrtfx . When you use an array (or some separate Range class) you’re creating a reference to a completely separate object, and one whose length is effectively unknown until the JRE starts optimizing. When you use two separate variables, their values are embedded directly in the body of their class or stack frame, and the compiler/JRE can see everything except the eventual run-time value beforehand. If you need to pass ranges around between methods or return them from a public method, you’ll either have to take two separate arguments as int min, int max or bite the bullet and come up with some class Range to bundle them up. You can also bundle two ints up into a long for internal passing-around, should the need arise: long bundle = (min & 0xFFFFFFFFL) | ((long)max << 32); int bundle_min = (int)bundle; int bundle_max = (int)(bundle >>> 32); In general—and Java makes this sort of hard in some places—it’s best to tie down your implementation as much as possible type-wise, so that neither you, nor users, nor other programmers can easily break things (e.g., by passing new int[0]). The types and variable names then serve as additional documentation, clarifying what the int values actually mean to the program.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range. Parameters: i - an int value. Returns: an Integer instance representing i. Since: 1.5 · public byte byteValue() Returns the value of this Integer as a byte after a narrowing ...
🌐
Oracle
docs.oracle.com › cd › E17802_01 › products › products › java-media › jai › forDevelopers › jai-apidocs › javax › media › jai › util › Range.html
Java Advanced Imaging: Class Range
A range is defined to contain all the values between the minimum and maximum values, where the minimum/maximum value can be considered either included or excluded from the range. This example creates a range of Integers whose minimum value is 1 and the maximum value is 5.
🌐
Squash
squash.io › how-to-generate-random-integers-in-a-range-in-java
How to Generate Random Integers in a Range in Java
November 3, 2023 - This code will generate and print a random integer between 1 and 100 (inclusive). Related Article: How to Print an ArrayList in Java · Alternatively, you can use the ThreadLocalRandom class, which is a subclass of Random specifically designed ...
🌐
Baeldung
baeldung.com › home › java › java numbers › listing numbers within a range in java
Listing Numbers Within a Range in Java | Baeldung
January 8, 2024 - The previous sections used a range to get a sequence of numbers. When we know how many numbers in a sequence is needed, we can utilize the IntStream.iterate: public List<Integer> getNumbersUsingIntStreamIterate(int start, int limit) { return IntStream.iterate(start, i -> i + 1) .limit(limit) .boxed() .collect(Collectors.toList()); }
🌐
TutorialsPoint
tutorialspoint.com › intstream-range-method-in-java
IntStream range() method in Java
July 30, 2019 - This returns a sequential ordered IntStream by an incremental step of 1 within the range − ... import java.util.*; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.range(20, 30); intStream.forEach(System.out::println); ...
🌐
Sentry
sentry.io › sentry answers › java › how do i generate random integers within a specific range in java?
How do I generate random integers within a specific range in Java? | Sentry
We can use the java.util.Random ... we’ll call the nextInt(int bound) method. This method returns an int in the range starting at 0 up to, but not including, the bound value we supply....
🌐
Coderanch
coderanch.com › t › 658900 › java › Array-range-numbers
Array range of numbers (Beginning Java forum at Coderanch)
December 10, 2015 - I'm trying to find a way to create an array that "CONTAINS" "NOT HOLD" primitive integers from 1 - 1000. So that i can have an array like . Hence i can point the number "2" in the array like number.length[1]. ... Let's have some 21st‑century programming We have been using Java8 for 21 months now.